我在SQLite中遇到查询问题。我无法实现目标,因为我收到语法错误。我正在尝试检查表帐户是否存在,如果不存在则创建它并添加一行。我已经尝试了几个代码,最后一个是:
IF NOT EXISTS (SELECT * FROM account) THEN
BEGIN
CREATE TABLE account (rowID INT, user VARCHAR(20), pass VARCHAR(20))
INSERT INTO account (rowID, user, pass) VALUES (0, '', '')
END
我正在使用SQLite 3和C#
我可以使用以下代码:
CREATE TABLE IF NOT EXISTS account (rowID INT, user VARCHAR(20), pass VARCHAR(20))
但我不知道如何添加插入新行的部分。如果此时创建了表,则必须仅插入该行,因此我无法在2个查询中执行此操作。
答案 0 :(得分:3)
尝试以下代码:
using (SQLiteConnection con = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;"))
using (SQLiteCommand command = con.CreateCommand())
{
con.Open();
command.CommandText = "SELECT name FROM sqlite_master WHERE name='account'";
var name = command.ExecuteScalar();
// check account table exist or not
// if exist do nothing
if (name != null && name.ToString() == "account")
return;
// acount table not exist, create table and insert
command.CommandText = "CREATE TABLE account (rowID INT, user VARCHAR(20), pass VARCHAR(20))";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO account (rowID, user, pass) VALUES (0, '', '')";
command.ExecuteNonQuery();
}
答案 1 :(得分:0)
{‘UpgradeOptional’:
{‘Pluginsucceeded’: ‘2020-06-06T12:00:09’,‘Submitplugin’: ‘2020-06-06T12:00:03’}}