如何使用(创建db,创建表,查询等)praeclarum sqlite-net?

Sya*_*hya 4 c# database sqlite sqlite-net

我想使用此链接https://github.com/praeclarum/sqlite-net提供的sqlite-net .

不幸的是,入门文档是不够的.它甚至没有提到如何创建数据库.我试着查看这些示例,遗憾的是,示例已被破坏(无法编译,运行时错误等).

我可以在网上找到的最实用的教程是http://blog.tigrangasparian.com/2012/02/09/getting-started-with-sqlite-in-c-part-one/

不幸的是,sqlite-net并不完全支持sqlite.org sqlite实现,因此使教程对praeclarum sqlite-net无用.

在教程中但在praeclarum sqlite-net中执行相同操作的等效方法是什么?

从教程

创建数据库(这是我卡住的地方)

SQLiteConnection.CreateFile("MyDatabase.sqlite");
Run Code Online (Sandbox Code Playgroud)

连接数据库

SQLiteConnection m_dbConnection;
m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
m_dbConnection.Open();
Run Code Online (Sandbox Code Playgroud)

创建表格

string sql = "create table highscores (name varchar(20), score int)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
Run Code Online (Sandbox Code Playgroud)

填表

string sql = "insert into highscores (name, score) values ('Me', 3000)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
sql = "insert into highscores (name, score) values ('Myself', 6000)";
command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
sql = "insert into highscores (name, score) values ('And I', 9001)";
command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
Run Code Online (Sandbox Code Playgroud)

查询数据库

string sql = "select * from highscores order by score desc";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
    Console.WriteLine("Name: " + reader["name"] + "\tScore: " + reader["score"]);
Run Code Online (Sandbox Code Playgroud)

Sla*_*hot 9

在你可以使用lambda的地方.这些类是强类型的.

让事情变得更加清洁.

如果你进入任何数量的数据缓存,你最终会希望你有像微软的同步框架一样在Mono中使用.我真的猜你的帖子,你正在考虑使用Xamarin.如果您要在本地缓存数据,请查看他们的SQLCipher组件.

此外,如果您通过组件商店使用SQLCipher ..它可以在Android 2.3上运行.因此,即使在项目中添加了支持库,也不要指望完全向后兼容的系统.

var db = new SQLiteConnection("sqllite.db")

db.CreateTable<SyncRecord> ();

db.Insert (new SyncRecord () { SyncDate = DateTime.UtcNow });

var query = db.Table<SyncRecord> ().Where( /* your lambda to filter*/);