C#SQLite命令行语法错误

时间:2017-02-26 06:38:02

标签: c# sql sqlite textbox controls

所以我试图将textbox和combobox控件中的文本插入到SQLite数据库中,但是我收到语法错误

private void btnConfirm_Click(object sender, EventArgs e)
    { 
        int indexID = 0;
        string username = txtUsername.Text;
        string password = txtPassword.Text;
        string firstName = txtFirstName.Text;
        string lastName = txtLastName.Text;
        int age = cmbAge.SelectedIndex + 1;
        string country = cmbCountry.Text;
        string city = txtCity.Text;
        string address = txtAddress.Text;
        string breeds = txtBreeds.Text;
        string notes = "None";

        SQLiteConnection registerConnection = new SQLiteConnection("Data Source=|DataDirectory|/Resources/database.sqlite;Version=3;");
        registerConnection.Open();
        SQLiteCommand registerCommand = new SQLiteCommand("INSERT INTO users (indexID,username,password,firstname,lastname,age,country,city,address,tigerbreeds,notes)", registerConnection);
        registerCommand.Parameters.AddWithValue("indexID", indexID); //0 for now, but we're going to change this later.
        registerCommand.Parameters.AddWithValue("username", username);
        registerCommand.Parameters.AddWithValue("password", password);
        registerCommand.Parameters.AddWithValue("firstname", firstName);
        registerCommand.Parameters.AddWithValue("lastname", lastName);
        registerCommand.Parameters.AddWithValue("age", age);
        registerCommand.Parameters.AddWithValue("country", country);
        registerCommand.Parameters.AddWithValue("city", city);
        registerCommand.Parameters.AddWithValue("address", address);
        registerCommand.Parameters.AddWithValue("tigerbreeds", breeds);
        registerCommand.Parameters.AddWithValue("tigerbreeds", notes);
        registerCommand.ExecuteNonQuery();
    }

有人知道如何解决这个问题吗?

System.Data.SQLite.dll中发生未处理的“System.Data.SQLite.SQLiteException”类型异常 附加信息:SQL逻辑错误或缺少数据库 在“)”附近:语法错误

2 个答案:

答案 0 :(得分:0)

尝试更新:

SQLiteCommand registerCommand = new SQLiteCommand("INSERT INTO users (indexID,username,password,firstname,lastname,age,country,city,address,tigerbreeds,notes) VALUES (@indexID, @username, @password, @firstname, @lastname, @age, @country, @city, @address, @tigerbreeds, @notes)", registerConnection);
registerCommand.Parameters.AddWithValue("@indexID", indexID); //0 for now, but we're going to change this later.
registerCommand.Parameters.AddWithValue("@username", username);
registerCommand.Parameters.AddWithValue("@password", password);
registerCommand.Parameters.AddWithValue("@firstname", firstName);
registerCommand.Parameters.AddWithValue("@lastname", lastName);
registerCommand.Parameters.AddWithValue("@age", age);
registerCommand.Parameters.AddWithValue("@country", country);
registerCommand.Parameters.AddWithValue("@city", city);
registerCommand.Parameters.AddWithValue("@address", address);
registerCommand.Parameters.AddWithValue("@tigerbreeds", breeds);
registerCommand.Parameters.AddWithValue("@notes", notes);
registerCommand.ExecuteNonQuery();

答案 1 :(得分:0)

您必须构造有效的SQL查询。 Insert(columnName)Values(@paramName)