简体   繁体   English

如何使用System.Data.SQLite和C#在代码中从头开始创建和持久化SQLite DB?

[英]How do I create and persist a SQLite DB from scratch in code using System.Data.SQLite and C#?

I have a database creation tool and am creating a database from scratch. 我有一个数据库创建工具,我从头开始创建一个数据库。 This is done the same way as mentioned Here . 这与在此处提到的方式相同。 It creates a temporary DB and I can add data to it. 它创建了一个临时数据库,我可以向其添加数据。

When I close the database it is deleted, which is the expected behavior, but I do not want it deleted. 当我关闭数据库时,它被删除,这是预期的行为,但我不希望它被删除。

I cannot find a Save or Create method in System.Data.SQLite and creating one with System.IO.File.Create(myDBFilename); 我在System.Data.SQLite中找不到Save或Create方法,并使用System.IO.File.Create(myDBFilename);创建一个方法System.IO.File.Create(myDBFilename); creates a file that SQLite cannot connect to. 创建一个SQLite无法连接的文件。

How do I create and persist a database in code from scratch? 如何从头开始在代码中创建和保留数据库?

Try this one, SQLiteConnection.CreateFile("c:\\mydatabasefile.db3"); 试试这个,SQLiteConnection.CreateFile(“c:\\ mydatabasefile.db3”);

if all else fails, here is command line stuff as well 如果所有其他方法都失败了,这里也是命令行的东西

sqlite3 test.db sqlite3 test.db
sqlite>CREATE TABLE cars ( id INTEGER PRIMARY KEY AUTOINCREMENT, model text, year integer ); sqlite> CREATE TABLE cars(id INTEGER PRIMARY KEY AUTOINCREMENT,model text,year integer);
sqlite>insert into cars( model, year ) values( “Ford 350″, 2007 ); sqlite>插入汽车(型号,年份)值(“福特350”,2007年);
sqlite>insert into cars( model, year ) values( “Buick Skylark”, 1953 ); sqlite>插入汽车(模型,年份)值(“别克Skylark”,1953年);
sqlite>insert into cars( model, year ) values( “Honda Civic”, 2002 ); sqlite>插入汽车(模型,年份)值(“Honda Civic”,2002);
sqlite>Select * from cars; sqlite>从汽车中选择*;
1|Ford 350|2007 1 |福特350 | 2007
2|Buick Skylark|1953 2 | Buick Skylark | 1953
3|Honda Civic|2002 3 | Honda Civic | 2002
sqlite>.quit sqlite的> .quit

You don't need to call the SQLiteConnection.CreateFile method. 您不需要调用SQLiteConnection.CreateFile方法。

Assuming you are opening a connection like this: 假设您正在打开这样的连接:

using (var connection = new SQLiteConnection("Data Source=C:\\database.db"))
{
    // foo
}

It will try to open the database file C:\\\\database.db if it exists, and if it doesn't exist, it will be created. 如果它存在,它将尝试打开数据库文件C:\\\\database.db ,如果它不存在,将创建它。

Assuming the use of c#-sqlite, 假设使用c#-sqlite,

when you create the connection, using the appropriate API, you actually pass in tbe name of the file to use as a parameter. 当您使用适当的API创建连接时,实际上传入文件的名称以用作参数。 If the file does not exist, then it is created (by default). 如果该文件不存在,则创建它(默认情况下)。 eg 例如

new SQLiteDatabase("c:\\foo.db")

Of course, because you'll need to escape the backslashes (this is because it's a string). 当然,因为你需要转义反斜杠(这是因为它是一个字符串)。 Creating a file beforehand does not work, because the resultant file is not an sqlite database, and therefore is not usable (so, I'm assuming you're getting an error in this case). 事先创建文件不起作用,因为生成的文件不是sqlite数据库,因此不可用(因此,我假设你在这种情况下遇到错误)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM