sqlite异常没有这样的列,只使用文本,现在我没有添加实数

时间:2014-08-12 14:27:28

标签: java android sql sqlite

我有一个sqlite数据库,它最初有3个文本字段并且工作正常,现在它有2个文本字段和两个真实字段并且它与'没有这样的列'崩溃,我重新安装了应用程序以摆脱任何旧表,我已经找了其他问题,找不到有同样问题的人。它在方法getAllScanAndLoc

的第一个查询中崩溃

SQLite Helper类:

package uk.co.kynesim.geoscanner;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MySQLiteHelper extends SQLiteOpenHelper {

public static final String TABLE_SCANS = "scanandloc";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_IMAGEFILE = "imagefile";
public static final String COLUMN_TIME = "time";
public static final String COLUMN_LATITUDE = "latitude";
public static final String COLUMN_LONGITUDE = "longitude";
private static final String DATABASE_NAME = "scanandloc.db";
private static final int DATABASE_VERSION = 1;

// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
        + TABLE_SCANS + "(" + COLUMN_ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_IMAGEFILE
        + " TEXT, " + COLUMN_TIME
        + " TEXT, " + COLUMN_LATITUDE + " REAL, " + COLUMN_LONGITUDE + " REAL);";

public MySQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase database) {
    database.execSQL(DATABASE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(MySQLiteHelper.class.getName(),
            "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_SCANS);
    onCreate(db);
}

}

数据源类:     包uk.co.kynesim.geoscanner;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;

public class ScanAndLocDataSource {

// Database fields
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = { MySQLiteHelper.COLUMN_ID, MySQLiteHelper.COLUMN_IMAGEFILE,
        MySQLiteHelper.COLUMN_TIME, MySQLiteHelper.COLUMN_LATITUDE, MySQLiteHelper.COLUMN_LONGITUDE };

public ScanAndLocDataSource(Context context) {
    dbHelper = new MySQLiteHelper(context);
}

public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
}

public void close() {
    dbHelper.close();
}

public ScanAndLoc createScanAndLoc(String imageFile, String time, Double latitude, Double longitude) {
    ContentValues values = new ContentValues();
    values.put(MySQLiteHelper.COLUMN_IMAGEFILE, imageFile);
    values.put(MySQLiteHelper.COLUMN_TIME, time);
    values.put(MySQLiteHelper.COLUMN_LATITUDE, latitude);
    values.put(MySQLiteHelper.COLUMN_LONGITUDE, longitude);
    long insertId = database.insert(MySQLiteHelper.TABLE_SCANS, null,
            values);
    Cursor cursor = database.query(MySQLiteHelper.TABLE_SCANS,
            allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
            null, null, null);
    cursor.moveToFirst();
    ScanAndLoc newScanAndLoc = cursorToScanAndLoc(cursor);
    cursor.close();
    return newScanAndLoc;
}

public void deleteScanAndLoc(ScanAndLoc scanAndLoc) {
    long id = scanAndLoc.getId();
    System.out.println("Comment deleted with id: " + id);
    database.delete(MySQLiteHelper.TABLE_SCANS, MySQLiteHelper.COLUMN_ID
            + " = " + id, null);
}

public ArrayList<ScanAndLoc> getAllScanAndLoc() {
    ArrayList<ScanAndLoc> scans = new ArrayList<ScanAndLoc>();

    Cursor cursor = database.query(MySQLiteHelper.TABLE_SCANS,
            allColumns, null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        ScanAndLoc scan = cursorToScanAndLoc(cursor);
        scans.add(scan);
        cursor.moveToNext();
    }
    // make sure to close the cursor
    cursor.close();
    return scans;
}

private ScanAndLoc cursorToScanAndLoc(Cursor cursor) {
    ScanAndLoc scan = new ScanAndLoc();
    scan.setId(cursor.getLong(0));
    scan.setImage(cursor.getString(1));
    scan.setDate(cursor.getString(2));
    scan.setLatitude(cursor.getInt(3));
    scan.setLatitude(cursor.getInt(4));
    return scan;

}

}

错误日志:

    08-12 15:22:33.802  23693-23693/uk.co.kynesim.geoscanner E/SQLiteLog﹕ (1) no such column: latitude
08-12 15:22:33.802  23693-23693/uk.co.kynesim.geoscanner E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: uk.co.kynesim.geoscanner, PID: 23693
java.lang.RuntimeException: Unable to start activity ComponentInfo{uk.co.kynesim.geoscanner/uk.co.kynesim.geoscanner.MainActivity}: android.database.sqlite.SQLiteException: no such column: latitude (code 1): , while compiling: SELECT _id, imagefile, time, latitude, longitude FROM scanandloc
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
 Caused by: android.database.sqlite.SQLiteException: no such column: latitude (code 1): , while compiling: SELECT _id, imagefile, time, latitude, longitude FROM scanandloc
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
        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:1314)
        at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
        at uk.co.kynesim.geoscanner.ScanAndLocDataSource.getAllScanAndLoc(ScanAndLocDataSource.java:58)
        at uk.co.kynesim.geoscanner.MainActivity.onCreate(MainActivity.java:59)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
         at android.os.Handler.dispatchMessage(Handler.java:102)
         at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)

2 个答案:

答案 0 :(得分:4)

仅重新安装应用程序并不足以摆脱旧数据库。

你必须

  • 删除旧应用程序的所有数据,然后安装新应用程序;或
  • 增加数据库版本(以便调用onUpgrade)。

答案 1 :(得分:2)

请参阅此CREATE TABLE命令:

private static final String DATABASE_CREATE = "create table "
        + TABLE_SCANS + "(" + COLUMN_ID
        + " integer primary key autoincrement," + COLUMN_IMAGEFILE
        + " image file, " + COLUMN_TIME
        + " time taken, "+COLUMN_LOCATION+" location image taken);";

错误

在SQLite中,没有这样的数据类型:image file也不是数据类型time taken,也不是数据类型location image taken

我假设你想写一些类似的东西:

private static final String DATABASE_CREATE = "create table "
        + TABLE_SCANS + "(" + COLUMN_ID
        + " integer primary key autoincrement," + COLUMN_IMAGEFILE
        + " TEXT, " + COLUMN_TIME + " INTEGER, "+COLUMN_LOCATION+" TEXT)";