请向我提供一些指导,我在这里遇到错误由SQL错误或缺少数据库引起

时间:2018-06-20 04:45:17

标签: android sql sqlite

我有一个错误:

  

java.lang.RuntimeException:无法启动活动   ComponentInfo {snmn.sportsconnectapplication / snmn.sportsconnectapplication.MainActivity}:   android.database.sqlite.SQLiteException:无此类列:名称(代码   1):,编译时:SELECT名称,locallatitude,locallongitude,   根据位置创建,日期验证,验证

     

错误代码:1(SQLITE_ERROR)    引起原因:SQL(query)错误或数据库丢失。         (无此列:名称(代码1):,而在编译时:SELECT名称,locallatitude,locallongitude,createby,dateverified,verified FROM   位置)

我的代码

public static final int DATABASE_VERSION =1;

private static final String CREATE_TABLE = " CREATE TABLE " + 
    DbContract.TABLE_NAME + "(" +
    " _id integer primary key autoincrement, " +
        DbContract.NAME + "text, " +
        DbContract.LOCAL_LATITUDE + "text, " +
        DbContract.LOCAL_LONGITUDE + "text, " +
        DbContract.VERIFIED + "boolean, " +
        DbContract.DATE_VERIFIED + "date, " +
        DbContract.CREATED_BY +
        "text);";`

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);
}

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

public void saveToLocalDatabase(String name, String locallatitude, String locallongitude, String createdby, Date dateverified, Boolean verified, SQLiteDatabase database){
    ContentValues contentValues = new ContentValues();
    contentValues.put(DbContract.LOCAL_LATITUDE,locallatitude);
    contentValues.put(DbContract.LOCAL_LONGITUDE,locallongitude);
    contentValues.put(DbContract.NAME,name);
    contentValues.put(DbContract.CREATED_BY,createdby);
    contentValues.put (DbContract.DATE_VERIFIED, String.valueOf(dateverified));
    contentValues.put(DbContract.VERIFIED,verified);
    database.insert(DbContract.TABLE_NAME,null,contentValues);
}
public Cursor readFromLocalDatabase (SQLiteDatabase database){
    String[] projection = {DbContract.NAME,DbContract.LOCAL_LATITUDE,DbContract.LOCAL_LONGITUDE,DbContract.CREATED_BY,DbContract.DATE_VERIFIED,DbContract.VERIFIED};
    return (database.query(DbContract.TABLE_NAME,projection,null,null,null,null,null));

}

1 个答案:

答案 0 :(得分:0)

您需要在列名和类型之间使用空格。例如,更改

DbContract.NAME + "text, " +

DbContract.NAME + " text, " +

解决这些问题后,您可以卸载并重新安装您的应用以重新触发onCreate()