首页 > 代码库 > sqllite
sqllite
public static void tt()
{
int ID = 0;
while(true)
{
string datasource = System.Environment.CurrentDirectory + "\\test.db";
if (!File.Exists(datasource))
{
System.Data.SQLite.SQLiteConnection.CreateFile(datasource);
}
System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection();
System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
StringBuilder sb = new StringBuilder();
try
{
System.Data.SQLite.SQLiteConnectionStringBuilder connstr = new System.Data.SQLite.SQLiteConnectionStringBuilder();
connstr.DataSource = datasource;
connstr.Password = "admin";
conn.ConnectionString = connstr.ToString();
conn.Open();
string sql = string.Empty;
cmd.Connection = conn;
sql = "select count(*) from sqlite_master where type=‘table‘ and name=‘test‘;";
cmd.CommandText = sql;
using (System.Data.SQLite.SQLiteDataReader reader1 = cmd.ExecuteReader())
{
if (reader1.Read() && 0 == reader1.GetInt16(0))
{
reader1.Dispose();
//创建表
sql = "CREATE TABLE test(id int,username varchar(20),password varchar(20))";
cmd.CommandText = sql;
cmd.Connection = conn;
cmd.ExecuteNonQuery();
}
}
//插入数据
sql = string.Format("INSERT INTO test VALUES({0},‘a‘,‘b‘)",++ID);
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
//取出数据
sql = "SELECT * FROM test";
cmd.CommandText = sql;
using (System.Data.SQLite.SQLiteDataReader reader = cmd.ExecuteReader())
{
int count = 0;
while (reader.Read())
{
sb.Append("id:").Append(reader.GetInt64(0)).Append(",");
sb.Append("username:").Append(reader.GetString(1)).Append(",")
.Append("password:").Append(reader.GetString(2)).Append("\n");
}
}
}
finally
{
cmd.Dispose();
conn.Dispose();
}
Console.WriteLine(sb.ToString());
Thread.Sleep(0);
}