我在android中创建了一个表。当我尝试添加或接收任何数据时,它会崩溃..可能是什么问题?
崩溃的代码:
return db.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_STRING,KEY_rewID },
null, null, null, null, null);
我在这里添加一行:
database.insert("1", encodedImageString, "8");
System.out.println(sqlite_obj.getAllData());
log cat:
04-24 01:23:58.110: E/AndroidRuntime(9766): android.database.sqlite.SQLiteException: no such table: photos (code 1): , while compiling: SELECT id, photostring, reviewID FROM photos
04-24 01:23:58.110: E/AndroidRuntime(9766): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-24 01:23:58.110: E/AndroidRuntime(9766): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
04-24 01:23:58.110: E/AndroidRuntime(9766): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
04-24 01:23:58.110: E/AndroidRuntime(9766): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
04-24 01:23:58.110: E/AndroidRuntime(9766): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
04-24 01:23:58.110: E/AndroidRuntime(9766): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
04-24 01:23:58.110: E/AndroidRuntime(9766): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
04-24 01:23:58.110: E/AndroidRuntime(9766): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
04-24 01:23:58.110: E/AndroidRuntime(9766): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
04-24 01:23:58.110: E/AndroidRuntime(9766): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
04-24 01:23:58.110: E/AndroidRuntime(9766): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
04-24 01:23:58.110: E/AndroidRuntime(9766): at com.project.sqlite.PhoDB.getAllData(PhoDB.java:95)
04-24 01:23:58.110: E/AndroidRuntime(9766): at com.project.simplify.Photo_upload$3.onClick(Photo_upload.java:133)
04-24 01:23:58.110: E/AndroidRuntime(9766): at android.view.View.performClick(View.java:4438)
04-24 01:23:58.110: E/AndroidRuntime(9766): at android.view.View$PerformClick.run(View.java:18422)
04-24 01:23:58.110: E/AndroidRuntime(9766): at android.os.Handler.handleCallback(Handler.java:733)
04-24 01:23:58.110: E/AndroidRuntime(9766): at android.os.Handler.dispatchMessage(Handler.java:95)
04-24 01:23:58.110: E/AndroidRuntime(9766): at android.os.Looper.loop(Looper.java:136)
04-24 01:23:58.110: E/AndroidRuntime(9766): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-24 01:23:58.110: E/AndroidRuntime(9766): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 01:23:58.110: E/AndroidRuntime(9766): at java.lang.reflect.Method.invoke(Method.java:515)
04-24 01:23:58.110: E/AndroidRuntime(9766): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-24 01:23:58.110: E/AndroidRuntime(9766): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-24 01:23:58.110: E/AndroidRuntime(9766): at dalvik.system.NativeStart.main(Native Method)
public class PhoDB {
public static final String KEY_ID = "id";
public static final String KEY_STRING = "photostring";
public static final String KEY_rewID = "reviewID";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "PhotoDB";
private static final String DATABASE_TABLE = "photos";
private static final int DATABASE_VERSION = 99;
private static final String DATABASE_CREATE =
"create table sample (id text primary key,reviewID text not null ,photostring text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public PhoDB(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS sample");
onCreate(db);
}
}
//---open SQLite DB---
public PhoDB open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
//---close SQLite DB---
public void close() {
DBHelper.close();
}
//---insert data into SQLite DB---
public long insert(String id, String photo_string, String reviewID) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ID, id);
initialValues.put(KEY_STRING, photo_string);
initialValues.put(KEY_rewID, reviewID);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---Delete All Data from table in SQLite DB---
public void deleteAll() {
db.delete(DATABASE_TABLE, null, null);
}
//---Get All Contacts from table in SQLite DB---
public Cursor getAllData() {
return db.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_STRING,KEY_rewID },
null, null, null, null, null);
}
}
答案 0 :(得分:1)
您正在创建具有不同名称的表格。
更改:
private static final String DATABASE_CREATE =
"create table sample (id text primary key,reviewID text not null ,photostring text not null);";
with:
private static final String DATABASE_CREATE =
"create table " + DATABASE_TABLE +" (id text primary key,reviewID text not null ,photostring text not null);";