【问题标题】:How to use(create db, create table, query, etc) praeclarum sqlite-net?如何使用(创建数据库,创建表,查询等)praeclarum sqlite-net?
【发布时间】:2013-11-08 03:42:50
【问题描述】:

我想使用这个链接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");

连接数据库

SQLiteConnection m_dbConnection;
m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
m_dbConnection.Open();

创建表

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

填表

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();

查询数据库

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"]);

【问题讨论】:

  • edit您的问题标题。正如它现在所读的那样,您的问题是查找教程的请求,而该问题在这里是题外话。 (要求链接到场外位置的问题是不恰当的。)看起来这不是您真正要问的,您应该修改标题以更准确地反映您的问题。如果你不这样做,它可能很快就会关闭。
  • 您愿意接受替代品吗? using this 怎么样?我在一个项目中使用它,效果很好。
  • @KenWhite 会的。谢谢。
  • @Noctis 虽然我对替代方案持开放态度,但我想学习 praeclarum sqlite-net 的原因是因为它是多平台的(ios、android、windows 手机)。谢谢。
  • 大声笑...刚刚意识到您正在使用我几个月前在研究 sqlite 时使用的教程:)。快速浏览一下 pareclarum,您必须为您的数据库创建类,它会根据它们为您创建表(我认为这很酷)

标签: c# database sqlite sqlite-net


【解决方案1】:

在您可以使用 lambda 的地方。这些类是强类型的。

让事情变得更干净。

如果您涉及任何数量的数据缓存,您最终会希望您有类似 Microsoft 的同步框架之类的东西可以在 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*/);

【讨论】:

  • 谢谢。语法完全不同。节省了我很多时间。
  • 已接受。你能帮我就我建议的答案进行一般 SQL 查询吗?谢谢。
  • 我没有看到 CreateTable 方法,我正在使用 SQLiteConnection 对象,我做错了吗?
  • @CoffeeAddict,你在使用 SQLLiteNet 吗?
【解决方案2】:

我建议的答案基于@Slack-Shot 的回复。

我尝试将教程转换为 praeclarum sqlite 语法兼容,以便参考像我这样的其他超级菜鸟。

创建和/或连接到数据库

private string dbPath = System.IO.Path.Combine
    (System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
     "MyDatabase.sqlite");

using (var m_dbConnection = new SQLite.SQLiteConnection(dbPath)) {}

创建表

public class highscore
{
    [MaxLength(20)]
    public string name { get; set; }
    public int score { get; set; }
}

using (var m_dbConnection = new SQLite.SQLiteConnection(dbPath))
{
    m_dbConnection.CreateTable<highscore>();
}

填表

using (var m_dbConnection = new SQLite.SQLiteConnection(dbPath))
{
    m_dbConnection.Insert(new highscore()
    {
        name = "Me",
        score = 9001
    });
    m_dbConnection.Insert(new highscore()
    {
        name = "Me",
        score = 3000
    });
    m_dbConnection.Insert(new highscore()
    {
        name = "Myself",
        score = 6000
    });
    m_dbConnection.Insert(new highscore()
    {
        name = "And I",
        score = 9001
    });
}

查询数据库

假设我有一个简单的 SQL 字符串,如下所示: "select * from highscores order by score desc"

如何以这种形式显示:

for(int i = 0; i < totalDataQueried; i++)
    Console.WriteLine("Name: " + name[i] + "\tScore: " + score[i]);

【讨论】:

  • 首先,阅读lambda expressions 的 3 分钟简介。那么,接下来您需要/想要查询什么?
  • var items = m_dbConnection.Table().OrderByDescending(t => t.score);这是一个按降序排列的 lambda 表达式。
  • 然后,你可以使用类似.. foreach(highscore hs in items) Console.WriteLine("Name: "+hs.name+"\tScore:"+hs.score);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-27
  • 2020-12-21
  • 1970-01-01
  • 1970-01-01
  • 2013-02-23
  • 2012-09-15
  • 1970-01-01
相关资源
最近更新 更多