未找到列 - Sqlite Android

时间:2015-06-03 17:03:03

标签: java android sqlite

我在这里搜索过多,但无法解决错误!

我有2个班级

public class DatabaseOperation extends SQLiteOpenHelper{
    private static final int DATABASE_VERSION = 2;

    public String QUERY = "CREATE TABLE " + TableData.TableInfo.TABLE_NAME +"("+ TableData.TableInfo.USER_NAME+"TEXT,"+
            TableData.TableInfo.USER_EMAIL+"TEXT,"+ TableData.TableInfo.USER_NUMBER+"TEXT );";
    public DatabaseOperation(Context context){
        super(context, TableData.TableInfo.DATABASES_NAME,null,DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL(QUERY);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){

    }
    public void putInfo(DatabaseOperation dop,String name,String email,String pass,String no){
        SQLiteDatabase sq = dop.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(TableData.TableInfo.USER_NAME,name);
        cv.put(TableData.TableInfo.USER_EMAIL,email);
        cv.put(TableData.TableInfo.USER_NUMBER,no);
//        cv.put(TableData.TableInfo.USER_PASS,pass);
        cv.put(TableData.TableInfo.USER_STATE,"Available");
        long k = sq.insert(TableData.TableInfo.TABLE_NAME,null,cv);
    }

    public boolean seacrhEmail(DatabaseOperation db,String email){

        return true;
    }
}

public class TableData {

    public static abstract class TableInfo implements BaseColumns{
        public static final String USER_NAME = "user_name";
        public static final String USER_EMAIL = "user_eMail";
//        public static final String USER_PASS = "user_pass";
        public static final String USER_NUMBER = "user_phNumber";
        public static final String DATABASES_NAME = "user_info";
        public static final String TABLE_NAME = "taxi_registration";
        public static final String USER_STATE = "taxi_state";
    }
}
运行插入语句时

错误

06-03 21:57:26.459    3700-3700/com.example.jawad.testapplication E/SQLiteLog﹕ (1) table taxi_registration has no column named user_name
06-03 21:57:26.475    3700-3700/com.example.jawad.testapplication E/SQLiteDatabase﹕ Error inserting user_name=bb user_eMail=bb@z.n taxi_state=Available user_phNumber=666
    android.database.sqlite.SQLiteException: table taxi_registration has no column named user_name (code 1): , while compiling: INSERT INTO taxi_registration(user_name,user_eMail,taxi_state,user_phNumber) VALUES (?,?,?,?)
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504)
            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:1492)
            at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1364)
            at com.example.jawad.testapplication.d.a(Unknown Source)
            at com.example.jawad.testapplication.r.onClick(Unknown Source)
            at android.view.View.performClick(View.java:4463)
            at android.view.View$PerformClick.run(View.java:18770)
            at android.os.Handler.handleCallback(Handler.java:808)
            at android.os.Handler.dispatchMessage(Handler.java:103)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5292)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
            at dalvik.system.NativeStart.main(Native Method)

我跟着this tutorial ...我不知道我错过了什么......

PS:我对android有点新鲜。

2 个答案:

答案 0 :(得分:3)

我相信问题很简单。

public static final String USER_NAME = "user_name";

我相信你找不到简单的列,因为它的名字不同。

在创建表后,您的用户名列的名称不是

  

USER_NAME

但是

  

user_nameTEXT

因为名称和列类型之间没有空格。

public String QUERY = "CREATE TABLE " + TableData.TableInfo.TABLE_NAME +"("+ TableData.TableInfo.USER_NAME+"TEXT,"+
        TableData.TableInfo.USER_EMAIL+"TEXT,"+ TableData.TableInfo.USER_NUMBER+"TEXT );";

尝试在列类型之前添加空格。 像这样:

public String QUERY = "CREATE TABLE " + TableData.TableInfo.TABLE_NAME +"("+ TableData.TableInfo.USER_NAME+" TEXT,"+
        TableData.TableInfo.USER_EMAIL+" TEXT,"+ TableData.TableInfo.USER_NUMBER+" TEXT );";

答案 1 :(得分:2)

为SQLite创建脚本添加空格

public String QUERY = "CREATE TABLE " + TableData.TableInfo.TABLE_NAME +"("+ TableData.TableInfo.USER_NAME+" TEXT,"+
        TableData.TableInfo.USER_EMAIL+" TEXT,"+ TableData.TableInfo.USER_NUMBER+" TEXT );";

在此之后,您应该将DATABASE_VERSION从2增加到3,以便执行sqlite的升级方法

private static final int DATABASE_VERSION = 3;

在升级数据库事件中,您应该删除表并再次创建它。

@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
      db.execSQL("DROP TABLE IF EXISTS " + TableData.TableInfo.TABLE_NAME);
      db.execSQL(QUERY);
}