SQLiteException:没有这样的列:firstname(代码1):

时间:2019-02-18 07:55:50

标签: android performance android-layout android-intent

我正在使用数据库,并且具有以下DbHelper类。 我是SQLite数据库的初学者

public class DbHelper extends SQLiteOpenHelper
{
    private static final int DATABASE_VERSION = 1;
    private static final String CREATE_TABLE =
            "CREATE TABLE "+DbContract.TABLE_NAME
            +"("+DbContract.COLUMN_ID +" INTEGER PRIMARY KEY AUTOINCREMENT,"
            +DbContract.COLUMN_FIRST_NAME+" text,"
            +DbContract.COLUMN_LAST_NAME+" text,"
            +DbContract.COLUMN_MOBILE_NUMBER+" text,"
            +DbContract.COLUMN_SYNC_STATUS+" integer)";

    private static final String DROP_TABLE = "drop table if exists "+DbContract.TABLE_NAME;

    public DbHelper(Context context)
    {
        super(context,DbContract.DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL(CREATE_TABLE);
        //Log.d("createTable", "Table is Creater: ");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        db.execSQL(DROP_TABLE);
        onCreate(db);
    }

    public void saveToLocalDatabase(String Fname, String Lname, String MobileNumber,int SyncStatus,SQLiteDatabase sqLiteDatabase)
    {
        ContentValues contentValues = new ContentValues();
        contentValues.put(DbContract.COLUMN_FIRST_NAME,Fname);
        contentValues.put(DbContract.COLUMN_LAST_NAME,Lname);
        contentValues.put(DbContract.COLUMN_MOBILE_NUMBER,MobileNumber);
        contentValues.put(DbContract.COLUMN_SYNC_STATUS,SyncStatus);

        sqLiteDatabase.insert(DbContract.TABLE_NAME,null,contentValues);
    }

    public Cursor readFromLocalDatabase(SQLiteDatabase sqLiteDatabase)
    {
        String[] projection = {DbContract.COLUMN_FIRST_NAME, DbContract.COLUMN_LAST_NAME, DbContract.COLUMN_MOBILE_NUMBER, DbContract.COLUMN_SYNC_STATUS};

        return (sqLiteDatabase.query(DbContract.TABLE_NAME,projection,null,null,null,null,null));
    }

    public void updateLocalDatabase(String Fname, String Lname, String MobileNumber, int SyncStatus,SQLiteDatabase sqLiteDatabase)
    {
        ContentValues contentValues = new ContentValues();
        contentValues.put(DbContract.COLUMN_SYNC_STATUS, SyncStatus);

        String selection = DbContract.COLUMN_FIRST_NAME + " LIKE ?" +DbContract.COLUMN_LAST_NAME+" LIKE ?" +DbContract.COLUMN_MOBILE_NUMBER+" LIKE ?";
                String[] selection_args = {Fname,Lname,MobileNumber};
                sqLiteDatabase.update(DbContract.TABLE_NAME, contentValues, selection, selection_args);
    }
    }

Logcat节目:

02-18 13:24:36.248 8946-8946/com.sbit.rahul.syncdemo E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.sbit.rahul.syncdemo, PID: 8946
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sbit.rahul.syncdemo/com.sbit.rahul.syncdemo.MainActivity}: android.database.sqlite.SQLiteException: no such column: firstname (code 1): , while compiling: SELECT firstname, lastname, mobilenumber, syncstatus FROM contactinfo
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2682)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1512)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:207)
        at android.app.ActivityThread.main(ActivityThread.java:5811)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681)
     Caused by: android.database.sqlite.SQLiteException: no such column: firstname (code 1): , while compiling: SELECT firstname, lastname, mobilenumber, syncstatus FROM contactinfo
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:896)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:507)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1346)
        at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1193)
        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1064)
        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1232)
        at com.sbit.rahul.syncdemo.database.DbHelper.readFromLocalDatabase(DbHelper.java:61)
        at com.sbit.rahul.syncdemo.MainActivity.readFromLocalStorage(MainActivity.java:86)
        at com.sbit.rahul.syncdemo.MainActivity.onCreate(MainActivity.java:62)
        at android.app.Activity.performCreate(Activity.java:6317)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2543)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2682) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1512) 
        at android.os.Handler.dispatchMessage(Handler.java:111) 
        at android.os.Looper.loop(Looper.java:207) 
        at android.app.ActivityThread.main(ActivityThread.java:5811) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681) 

1 个答案:

答案 0 :(得分:0)

日志清楚地表明

 Caused by: android.database.sqlite.SQLiteException: no such column: firstname (code 1): , while compiling: SELECT firstname, lastname, mobilenumber, syncstatus FROM contactinfo

您可以检查您的表结构吗,它有名字列吗?请检查所有列。