我在数据库中设置了一个新表。我有其他现有的表可以正常工作,但这个最新的表来得晚,并证明有问题。
有问题的表是条形表。我已经从SQLite数据库浏览器检查了数据库的结构,我看到了我期望的列。似乎有3种可能性。
1)数据库未正确创建 - 但我可以看到该列,所以我认为这不太可能?
2)我的输入法存在问题 - 我为其他表实现了类似的方法。
3)升级数据库版本时出现问题
我已经截断了大部分代码,只隔离了重要的部分。
public class DBmanager {
private static final String DATABASE_NAME = "DBmanagerdb";
private static final int DATABASE_VERSION = 1;
// Shared strings and main table
private static final String DATABASE_TABLE = "drinkTable";
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "drink_name";
public static final String KEY_BAR = "bar_name";
public static final String KEY_SCORE = "drink_score";
public static final String KEY_VOL = "volume";
public static final String KEY_PRICE = "price";
public static final String KEY_DRINKABV = "abv";
public static final String KEY_FAVE = "favorite";
public static final String MYTAB_DRINKCOUNT = "mytab_drinkCount";
// Strings for ABV
private static final String ABV_TABLE = "abvTable";
public static final String KEY_ABV_ID = "_id";
public static final String KEY_NAME_ABV = KEY_NAME;
public static final String KEY_ABV = "abv_value";
//
private static final String VERSION_TABLE = "versionTable";
public static final String VERSION_NUMBER = "version";
private static final String BAR_TABLE = "barTable";
private static final String KEY_LAT = "barLatitude";
private static final String KEY_LONG = "barLongitude";
public static final String KEY_BARID = "barId";
// Strings for My Tab
;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_BAR
+ " TEXT NOT NULL, " + KEY_NAME + " TEXT NOT NULL, "
+ KEY_PRICE + " DOUBLE, " + KEY_VOL + " DOUBLE, "
+ KEY_DRINKABV + " DOUBLE, " + KEY_SCORE + " DOUBLE,"
+ MYTAB_DRINKCOUNT + " INTEGER," + KEY_FAVE + " INTEGER);");
db.execSQL("CREATE TABLE " + ABV_TABLE + " (" + KEY_ABV_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME_ABV
+ " TEXT NOT NULL, " + KEY_ABV + " DOUBLE);");
db.execSQL("CREATE TABLE " + VERSION_TABLE + " (" + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + VERSION_NUMBER
+ " INTEGER);");
db.execSQL("CREATE TABLE " + BAR_TABLE + " (" + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_BAR
+ " TEXT NOT NULL, " + KEY_LAT + " TEXT NOT NULL, " + KEY_LONG
+ " TEXT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
newVersion = oldVersion;
Log.i("Values", "OldVersion: " + oldVersion + " New Version: "
+ newVersion);
db.execSQL("DROP TABLE IF EXISTS " + BAR_TABLE);
onCreate(db);
}
}
public long enterNewBar(String barName) {
ContentValues cv = new ContentValues();
cv.put(KEY_BAR, barName);
cv.put(KEY_LAT, "hello");
cv.put(KEY_LONG, "world");
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public boolean checkUniqueBar(String barName) {
boolean flag = false;
String myQuery = "SELECT DISTINCT " + KEY_BAR + " FROM "
+ DATABASE_TABLE;
Cursor c = ourDatabase.rawQuery(myQuery, null);
if (c != null) {
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
if (c.getString(c.getColumnIndex(KEY_BAR)) == barName) {
flag = true;
}
}
c.close();
}
return flag;
}
堆栈跟踪:
05-22 00:53:56.859: E/SQLiteLog(8496): (1) table drinkTable has no column named barLongitude
05-22 00:53:56.909: E/SQLiteDatabase(8496): Error inserting barLongitude=world barLatitude=hello bar_name=TEst
05-22 00:53:56.909: E/SQLiteDatabase(8496): android.database.sqlite.SQLiteException: table drinkTable has no column named barLongitude (code 1): , while compiling: INSERT INTO drinkTable(barLongitude,barLatitude,bar_name) VALUES (?,?,?)
05-22 00:53:56.909: E/SQLiteDatabase(8496): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
05-22 00:53:56.909: E/SQLiteDatabase(8496): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
05-22 00:53:56.909: E/SQLiteDatabase(8496): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
05-22 00:53:56.909: E/SQLiteDatabase(8496): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
05-22 00:53:56.909: E/SQLiteDatabase(8496): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
05-22 00:53:56.909: E/SQLiteDatabase(8496): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
05-22 00:53:56.909: E/SQLiteDatabase(8496): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
05-22 00:53:56.909: E/SQLiteDatabase(8496): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
05-22 00:53:56.909: E/SQLiteDatabase(8496): at mjd.mdflip.areyoudrunk.DBmanager.enterNewBar(DBmanager.java:555)
05-22 00:53:56.909: E/SQLiteDatabase(8496): at mjd.mdflip.areyoudrunk.fragments.MainFrag.calculateScore(MainFrag.java:288)
05-22 00:53:56.909: E/SQLiteDatabase(8496): at mjd.mdflip.areyoudrunk.fragments.MainFrag.onClick(MainFrag.java:145)
05-22 00:53:56.909: E/SQLiteDatabase(8496): at android.view.View.performClick(View.java:4204)
05-22 00:53:56.909: E/SQLiteDatabase(8496): at android.view.View$PerformClick.run(View.java:17355)
05-22 00:53:56.909: E/SQLiteDatabase(8496): at android.os.Handler.handleCallback(Handler.java:725)
05-22 00:53:56.909: E/SQLiteDatabase(8496): at android.os.Handler.dispatchMessage(Handler.java:92)
05-22 00:53:56.909: E/SQLiteDatabase(8496): at android.os.Looper.loop(Looper.java:137)
05-22 00:53:56.909: E/SQLiteDatabase(8496): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-22 00:53:56.909: E/SQLiteDatabase(8496): at java.lang.reflect.Method.invokeNative(Native Method)
05-22 00:53:56.909: E/SQLiteDatabase(8496): at java.lang.reflect.Method.invoke(Method.java:511)
05-22 00:53:56.909: E/SQLiteDatabase(8496): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-22 00:53:56.909: E/SQLiteDatabase(8496): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-22 00:53:56.909: E/SQLiteDatabase(8496): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您正在插入drinkTable
,而不是barTable
:
更改
return ourDatabase.insert(DATABASE_TABLE, null, cv);
到
return ourDatabase.insert(BAR_TABLE, null, cv);
编写方法的更好方法是:
public boolean checkUniqueBar(String barName) {
boolean flag = false;
String myQuery = "SELECT COUNT(*) " FROM "
+ DATABASE_TABLE + " WHERE " + KEY_BAR + "=?";
Cursor c = ourDatabase.rawQuery(myQuery, new String[] { barName } );
if (c != null) {
c.moveToFirst();
if( c.getInteger(0) > 0 )
flag = true;
c.close();
}
return flag;
}