SQLite 不保存我插入的数据 [英] SQLite doesn't save data I inserted
问题描述
我正在使用 SQL lite 并学习它.
I'm messing around with SQL lite and learning it.
我有一个名为 People 的表,我有一些方法可以连接到数据库并执行一些操作,例如显示所有信息.
I got a table called People, I got some method that connect to the database and do some stuff, like show all the info.
现在我正在尝试插入一些数据,但在这里它对我来说很奇怪.我有这个方法:
Now I'm trying to insert some data and here it get wierd for me. I have this method:
private void ExecuteQuery(string txtQuery)
{
SetConnection();
sql_con.Open();
sql_cmd = sql_con.CreateCommand();
sql_cmd.CommandText = txtQuery;
sql_cmd.ExecuteNonQuery();
sql_con.Close();
}
并查看我使用此方法的所有数据:
and to see all the data I've got this method:
private void LoadData()
{
SetConnection();
sql_con.Open();
sql_cmd = sql_con.CreateCommand();
string CommandText = "SELECT * FROM People";
DB = new SQLiteDataAdapter(CommandText, sql_con);
DS.Reset();
DB.Fill(DS);
DT = DS.Tables[0];
dataGridView1.DataSource = DT;
sql_con.Close();
}
当我插入一些数据并紧接着调用LoadData()
时,我可以看到我所做的所有更改.
When I inset some data and right afther it I call the LoadData()
, I can see all the changes I made.
关闭程序,然后再次打开并调用LoadData()
后,我看不到之前插入的新信息.
After I close the program, and then open it agian and call LoadData()
, I don't see the new info that I inserted before.
我得到了一些使用 SQL lite GUI 应用程序插入的数据,每次调用 LoadData()
方法时我都可以看到这些数据,但不是我的.
I got some data that I used SQL lite GUI app to insert, and I can see that data every time I call the LoadData()
method, but not mine.
我是否需要做其他事情来确保 SQL lite 保存所有数据?
Do I need to do somthing else to make sure SQL lite saves all the data?
这就是我插入数据的方式,mybee 问题出在这里?
string insetCommand = "INSERT INTO People Values(2,'" + textBox1.Text + "')";
我的 connectionString 方法
public static string connectionString
{
get
{
string database =
//AppDomain.CurrentDomain.BaseDirectory + "..\\..\\Database\\ImageLib.s3db";
AppDomain.CurrentDomain.BaseDirectory + "\\DataBase\\myTable2";
string connectionString =
@"Data Source=" + Path.GetFullPath(database) + ";Version=3;";
return connectionString;
}
}
推荐答案
您的问题看起来好像您的数据存储在临时数据库中.
Your problem looks as if your data is stored in a temporary database.
请检查您的 connectionString
是否实际使用(如果文件名为空,SQLite 将使用临时数据库).
Please check that your connectionString
is actually used (SQLite will use a temporary database if the file name is empty).
要从应用程序内部检查数据库文件名,请执行 PRAGMA database_list
像查询一样检查结果的第三列是否包含您的文件名.示例输出:
To check the database file name from inside your application, execute PRAGMA database_list
like a query and check that the third column of the result contains your file name. Example output:
> pragma database_list;
seq name file
----- ------ ----------
0 main /tmp/test.db
这篇关于SQLite 不保存我插入的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!