C# 如何使用(创建数据库、创建表、查询等)praeclarum sqlite-net?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19851213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use(create db, create table, query, etc) praeclarum sqlite-net?
提问by Syaiful Nizam Yahya
I want to use sqlite-net available from this link https://github.com/praeclarum/sqlite-net.
我想使用此链接https://github.com/praeclarum/sqlite-net提供的 sqlite-net 。
Unfortunately, the getting started documentation are not enough. It doesnt even mention how to create a database. I tried looking at the examples, unfortunately, the examples are broken(unable to compile, run time error etc).
不幸的是,入门文档还不够。它甚至没有提到如何创建数据库。我尝试查看示例,不幸的是,示例已损坏(无法编译、运行时错误等)。
The most practical tutorial i can find on the net is http://blog.tigrangasparian.com/2012/02/09/getting-started-with-sqlite-in-c-part-one/
我在网上能找到的最实用的教程是http://blog.tigrangasparian.com/2012/02/09/getting-started-with-sqlite-in-c-part-one/
Unfortunately, sqlite-net doesnt fully support sqlite.org sqlite implementation, thus making the tutorial useless for praeclarum sqlite-net.
不幸的是,sqlite-net 并不完全支持 sqlite.org sqlite 实现,因此本教程对 praeclarum sqlite-net 毫无用处。
What is the equivalent method to do the same thing from the tutorial but in praeclarum sqlite-net?
从教程中但在 praeclarum sqlite-net 中执行相同操作的等效方法是什么?
From the tutorial
从教程
Create database(Here is where i stuck)
创建数据库(这是我卡住的地方)
SQLiteConnection.CreateFile("MyDatabase.sqlite");
Connect to database
连接到数据库
SQLiteConnection m_dbConnection;
m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
m_dbConnection.Open();
Create table
创建表
string sql = "create table highscores (name varchar(20), score int)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
Fill table
填表
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();
Query database
查询数据库
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"]);
采纳答案by Slack Shot
In your Where you can use lambdas. The classes are strongly typed.
在您可以使用 lambda 的地方。这些类是强类型的。
Makes things a lot cleaner.
使事情变得更干净。
If you get into any amount of data caching, you'll end up wishing you had something like Microsoft's sync framework to use in Mono. I'm really guessing by your post, that you are looking at using Xamarin. Take a look at their SQLCipher component, if you are going to be caching data locally.
如果您涉及任何数量的数据缓存,您最终会希望自己能在 Mono 中使用类似 Microsoft 的同步框架之类的东西。我真的从你的帖子中猜测,你正在考虑使用 Xamarin。如果您要在本地缓存数据,请查看他们的 SQLCipher 组件。
Also, if you do use SQLCipher via the component store.. it works in Android 2.3 on up. So don't expect a fully backward compatible system even with the Support Library added to your project.
此外,如果您确实通过组件存储使用 SQLCipher ..它可以在 Android 2.3 上运行。因此,即使将支持库添加到您的项目中,也不要指望完全向后兼容的系统。
var db = new SQLiteConnection("sqllite.db")
var db = new SQLiteConnection("sqllite.db")
db.CreateTable<SyncRecord> ();
db.CreateTable<SyncRecord> ();
db.Insert (new SyncRecord () { SyncDate = DateTime.UtcNow });
db.Insert (new SyncRecord () { SyncDate = DateTime.UtcNow });
var query = db.Table<SyncRecord> ().Where( /* your lambda to filter*/);
var query = db.Table<SyncRecord> ().Where( /* your lambda to filter*/);
回答by Syaiful Nizam Yahya
My suggested answer is based on @Slack-Shot response.
我建议的答案基于@Slack-Shot 响应。
I try to convert the tutorial to be praeclarum sqlite syntax compatible for reference to other super noobs like me.
我尝试将教程转换为 praeclarum sqlite 语法兼容,以供像我这样的其他超级菜鸟参考。
Create and/or connect to database
创建和/或连接到数据库
private string dbPath = System.IO.Path.Combine
(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
"MyDatabase.sqlite");
using (var m_dbConnection = new SQLite.SQLiteConnection(dbPath)) {}
Create table
创建表
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>();
}
Fill table
填表
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
});
}
Query database
查询数据库
Assuming I have a simple SQL string like so: "select * from highscores order by score desc"
假设我有一个简单的 SQL 字符串,如下所示:“select * from highscores order by score desc”
How do I display it in this form:
我如何以这种形式显示它:
for(int i = 0; i < totalDataQueried; i++)
Console.WriteLine("Name: " + name[i] + "\tScore: " + score[i]);