我正在尝试从空表中获取数据并显示"没有这样的表错误"。 这是我的数据库适配器代码。
DBAdapter.java
package com.prome.testdatabase;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
/**
* @author Md. Nabid Imteaj
* @version 0.1
* @see Database adapter for creating local databases
* and also for local queries.
*/
public class DBAdapter {
// define tag
public static final String TAG = "DBAdapter";
// define table fields
static final String KEY_ID = "_id";
static final String KEY_NAME = "name";
// define table names
static final String TABLE_INFO = "info";
// define database name
static final String DATABASE_NAME = "test_db_debug";
// define database version
static final int DATABASE_VERSION = 1;
// define separators
static final String COMMA_SEP = ", ";
static final String IPA_SEP = " integer primary autoincrement ";
static final String INT_SEP = " integer";
static final String TEXT_SEP = " TEXT";
// define database creation queries
static final String CREATE_INFO = "create table " + TABLE_INFO + "( "
+ KEY_ID + IPA_SEP + COMMA_SEP
+ KEY_NAME + TEXT_SEP +" )";
final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;
// Constructor. Creates database.
public DBAdapter( Context ctx ) {
this.context = ctx;
// create database
DBHelper = new DatabaseHelper(context);
}
// ----- make private class -----
private class DatabaseHelper extends SQLiteOpenHelper {
// constructor
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
//Toast.makeText(context, "DatabaseHelper Constructor", Toast.LENGTH_LONG).show();
Log.d(TAG, "DatabseHelper Constructor Loaded.");
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
// create databases
try {
db.execSQL(CREATE_INFO);
Log.d(TAG, "All tables has been created.");
} catch(Exception e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.d( TAG, "Updating database from version " + oldVersion + " to " + newVersion +
", which will destroy all old data.");
String drp = "DROP TABLE IF EXISTS ";
db.execSQL( drp + TABLE_INFO );
onCreate(db);
}
} // ----- ends private class ------
// --- opens the database ---
public DBAdapter open() {
db = DBHelper.getWritableDatabase();
Log.d(TAG, "Database opened.");
return this;
}
// --- closes the database ---
public void close() {
DBHelper.close();
Log.d(TAG, "Database closed.");
}
// get all data of table
public Cursor getAllData() {
return db.query(TABLE_INFO, new String[]{ KEY_ID, KEY_NAME }, null, null, null, null, null);
}
}
在MainActivity.java中> onCreate()方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DBAdapter db = new DBAdapter(this);
db.open();
Cursor c = db.getAllData();
while( !c.isAfterLast() ) {
Log.d("DB", "id: "+c.getString(0)+", name: "+c.getString(1));
c.moveToNext();
}
db.close();
}
请解释缺陷的内容和位置并给出解决方案。提前谢谢。
答案 0 :(得分:2)
CREATE TABLE
中存在语法错误。 integer primary autoincrement
应为integer primary key autoincrement
。
修复语法错误后,卸载您的应用,以便重新创建数据库文件。
为什么它之前没有失败是因为你在onCreate()
中捕获异常。这是个坏主意。问题需要向上传播,以便SQLiteOpenHelper
不会认为数据库设置没有成功。