每次我需要调用我的方法RowID时,我都会收到No Such Table错误消息。我会接受它,如果桌子不存在,但它确实存在,所以我不知道它有什么问题。
以下是错误消息:
05-06 13:51:23.429 4633-4633/pt.smartgeo.aees E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: pt.smartgeo.aees, PID: 4633
android.database.sqlite.SQLiteException: no such table: redes (code 1): , while compiling: SELECT * FROM redes
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.rawQuery(SQLiteDatabase.java:1253)
at pt.smartgeo.aees.NetworksDBAdapter.RowID(NetworksDBAdapter.java:114)
at pt.smartgeo.aees.CreateNetwork$1.onClick(CreateNetwork.java:57)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
希望是这种方法:
public class NetworksDBAdapter {
private SQLiteDatabase database;
private NetworksDB networks;
private String[] allColumns = {NetworksDB.ID, NetworksDB.NETWORKD_ID, NetworksDB.NAME, NetworksDB.MACRO};
public NetworksDBAdapter(Context context) {
networks = new NetworksDB(context);
open();
}
public void open() throws SQLException {
database = networks.getWritableDatabase();
}
public Network createNetwork(String id, String name, int macro) {
ContentValues values = new ContentValues();
values.put(NetworksDB.NETWORKD_ID, id);
values.put(NetworksDB.NAME, name);
values.put(NetworksDB.MACRO, macro);
long insertId = database.insert(NetworksDB.TABLE_NAME, null, values);
// To show how to query
Cursor cursor = database.query(NetworksDB.TABLE_NAME, allColumns, PointsDB.ID + " = " +
insertId, null,null, null, null);
cursor.moveToFirst();
return cursorToPoint(cursor);
}
private Network cursorToPoint(Cursor cursor) {
Network network = new
Network(cursor.getString(1), cursor.getString(2), Integer.parseInt(cursor.getString(3)));
return network;
}
public void deleteNetwork(int idPoint){
database.delete(NetworksDB.TABLE_NAME, PointsDB.ID + " = " + idPoint,
null);
}
public void updateNetwork(int id, String idNetwork, String name, int macro){
String where = "_id=?";
String[] whereArgs = new String[] {String.valueOf(id)};
ContentValues values = new ContentValues();
values.put(NetworksDB.NETWORKD_ID, idNetwork);
values.put(NetworksDB.NAME, name);
values.put(NetworksDB.MACRO, macro);
database.update(PointsDB.TABLE_NAME, values, where, whereArgs);
}
public Cursor getNetworks(){
Cursor cursor = database.rawQuery("SELECT _id, NetworkID, name, macro from redes", null);
return cursor;
}
public Network getNetwork(int id){
Cursor cursor = database.query(PointsDB.TABLE_NAME, allColumns, NetworksDB.ID + " = " +
id, null,null, null, null);
if (cursor.moveToFirst()) {
return cursorToPoint(cursor);
}
return null;
}
public long count() {
return DatabaseUtils.queryNumEntries(database,NetworksDB.TABLE_NAME);
}
public List<String> RowID(){
List<String> List = new ArrayList<String>();
String selectQuery = "SELECT * FROM " + NetworksDB.TABLE_NAME;
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
List.add(cursor.getString(0));
} while (cursor.moveToNext());
}
return List;
}
}
顺便说一下,这是一句话:
String selectQuery = "SELECT * FROM " + NetworksDB.TABLE_NAME;
这是我的NetworksDB类
public class NetworksDB extends SQLiteOpenHelper {
public static final String ID = "_id";
public static final String NETWORKD_ID = "NetworkID";
public static final String NAME = "name";
public static final String MACRO = "macro";
private static final int DATABASE_VERSION = 3;
private static final String DATABASE_NAME = "aees.db";
public static final String TABLE_NAME = "redes";
private static final String DATABASE_CREATE = "create table "
+ TABLE_NAME + "( " + ID
+ " integer primary key autoincrement, " + NETWORKD_ID
+ " text not null, " + NAME + " text not null, "
+ MACRO + " text not null);";
public NetworksDB(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(PointsDB.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data"
);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
任何人都有任何想法吗? :/
答案 0 :(得分:2)
由于这一行,您的表格未被创建:
+ MACRO + " text not );";
应该是:
+ MACRO + " text not null);";
<强> [编辑] 强>
在您编辑问题之前,这是真的。
答案 1 :(得分:0)
基于评论清除应用数据有帮助。显然,您的设备已经有一个具有相同名称和版本但没有表的数据库文件。调用getWritableDatabase()
只是愉快地打开它,但没有调用onCreate()
或onUpgrade()
等生命周期方法。
进一步阅读:When is SQLiteOpenHelper onCreate() / onUpgrade() run?