首页 文章

C#SQLite命令行语法错误

提问于
浏览
0

所以我试图将文本框和组合框控件中的文本插入到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

    尝试更新到这个:

    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();
    
  • 0

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

相关问题