0

这是我创建数据库表的代码

public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_PH_NO + " TEXT" + KEY_CTRL_TYPE + " TEXT,"
            + KEY_USER_NAME + " TEXT,"+ KEY_PASSWORD + " TEXT "
            + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

这是用于添加联系人

void addContact(Accounts accounts) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, accounts.getName()); // Contact Name
    values.put(KEY_PH_NO, accounts.getPhoneNumber()); // Contact Phone
    values.put(KEY_CTRL_TYPE, accounts.getControlType()); //
    values.put(KEY_USER_NAME, accounts.getUserName()); //
    values.put(KEY_PASSWORD, accounts.getPassword()); //


    // Inserting Row
    db.insert(TABLE_CONTACTS, null, values);
    db.close(); // Closing database connection
}

我在访问时收到此错误addContact

db.addContact(new Accounts(siteName,siteNum,ctype,username,pass));

29206-29206/com.atrolabe.tcpremote1 E/SQLiteDatabase﹕ Error inserting control_type=SHAULA 720 user_name=wetyu phone_number=123455566 password=fhhchhjh name=test android.database.sqlite.SQLiteException: table accounts has no column named control_type (code 1): , while compiling: INSERT INTO accounts(control_type,user_name,phone_number,password,name) VALUES (?,?,?,?,?) at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)

logcat 在 addcontact 中指向这一行

db.insert(TABLE_CONTACTS, null, values);

4

1 回答 1

2

问题实际上出在初始onCreate()方法上。生成的 SQL 构造不正确,从不创建control_type列。见下文:

"CREATE TABLE " + TABLE_CONTACTS + "("
        + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
        + KEY_PH_NO + " TEXT" + KEY_CTRL_TYPE +    ...

在电话号码字段之后,关键字位于预期的列名称TEXT旁边;control_type导致意外的 SQL。例如(填写期望值)

CREATE TABLE Contacts (
            ContactID INTEGER PRIMARY KEY,  ContactName TEXT,
            ContactNumber TEXTcontrol_type  ...

要解决此问题,只需正确地重新格式化您的CREATE_CONTACTS_TABLE字符串。

于 2013-11-12T00:18:02.403 回答