android.database.sqlite.SQLiteException:表联系人没有名为uid的列

时间:2014-05-17 09:55:16

标签: android sqlite

这是我的代码:

public void onCreate(SQLiteDatabase db) 
{
    String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS" 
            + TABLE_CONTACTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," // and auto increment will be handled with                            primary key
            + KEY_UID + " TEXT,"
            + KEY_NAME + " TEXT," 
            + KEY_PH_NO + " TEXT,"
            + KEY_Comapny + " TEXT,"
            + KEY_email + " TEXT," 
            + KEY_country + " TEXT," 
            + KEY_street + " TEXT," 
            + KEY_city + " TEXT,"+ KEY_state + " TEXT," 
            + KEY_zip + " TEXT);";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

我得到以下异常:

: E/SQLiteDatabase(2645): android.database.sqlite.SQLiteException: table contacts has no column named uid (code 1): , while compiling: INSERT INTO contacts(uid,zip,phone_number,email,company_name,street,name,state,city,country) VALUES (?,?,?,?,?,?,?,?,?,?)

请帮忙。 感谢

3 个答案:

答案 0 :(得分:2)

CREATE TABLE中存在语法错误,所以很明显它还没有运行。您需要EXISTS和表名之间的空格。

由于尚未运行当前SQL的onCreate(),因此您的数据库文件版本较旧。卸载您的应用以将其删除,然后再次运行onCreate()。有关详情,请参阅When is SQLiteOpenHelper onCreate() / onUpgrade() run?

答案 1 :(得分:1)

替换此查询:

String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS" 
                + TABLE_CONTACTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," // and auto increment will be handled with                            primary key
                + KEY_UID + " TEXT,"
                + KEY_NAME + " TEXT," 
                + KEY_PH_NO + " TEXT,"
                + KEY_Comapny + " TEXT,"
                + KEY_email + " TEXT," 
                + KEY_country + " TEXT," 
                + KEY_street + " TEXT," 
                + KEY_city + " TEXT,"+ KEY_state + " TEXT," 
                + KEY_zip + " TEXT);";
        db.execSQL(CREATE_CONTACTS_TABLE);

用这个:

String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS " 
                + TABLE_CONTACTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," // and auto increment will be handled with                            primary key
                + KEY_UID + " TEXT,"
                + KEY_NAME + " TEXT," 
                + KEY_PH_NO + " TEXT,"
                + KEY_Comapny + " TEXT,"
                + KEY_email + " TEXT," 
                + KEY_country + " TEXT," 
                + KEY_street + " TEXT," 
                + KEY_city + " TEXT,"+ KEY_state + " TEXT," 
                + KEY_zip + " TEXT);";
        db.execSQL(CREATE_CONTACTS_TABLE);

注意:您的CREATE TABLE查询中存在语法错误,您需要EXISTS和TABLE_CONTACTS之间的空格。

答案 2 :(得分:0)

这是解决方案: 我遇到了同样的问题,现在我解决了

我们要创建数据库应用 你有两个java类 首先是参数 Params.java,另一个是Contact.java,我们在那里构造构造函数,获取和设置等等。

Param.java下面的类代码:(请参见第5和6行中的错误)并修复该引用

package com.andro.background.crud_db.param;

public class Params {
    public static final int DB_VERSION=1;
    public static final String DB_NAME="contacts_db"; //(-_-)check this, My mistake was "contact_db" , i write wrong name so it shows error
    public static final String TABLE_NAME="contacts_table"; // Right is "contact_table", this kinda error in your code

    //key of our table in db
    public static final String KEY_ID="id";
    public static final String KEY_NAME="name";
    public static final String KEY_PHONE="phone_number";
}

(在较高代码中检查此错误) 如果您犯了同样的错误,那么您的错误将类似于下面的代码

2020-02-03 02:57:24.432 10705-10705/com.andro.background.cruddb E/SQLiteLog: (1) table contacts_table has no column named phone_number
2020-02-03 02:57:24.434 10705-10705/com.andro.background.cruddb E/SQLiteDatabase: Error inserting name=sunnykanumber phone_number=9090909090
    android.database.sqlite.SQLiteException: table contacts_table has no column named phone_number (code 1): , while compiling: INSERT INTO contacts_table(name,phone_number) VALUES (?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:890)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:501)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1546)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1417)
        at com.andro.background.cruddb.data.MyDbHandler.addContact(MyDbHandler.java:62)
        at com.andro.background.cruddb.MainActivity.onCreate(MainActivity.java:31)
        at android.app.Activity.performCreate(Activity.java:7009)
        at android.app.Activity.performCreate(Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)