我在Android应用上查询SQLite数据库时遇到问题。我使用this tutorial来设置我的数据库助手类。
我做了一个简单的查询功能:
public Cursor queryDB(String Query,String[] args){
Cursor cursor = myDataBase.rawQuery(Query, args);
return cursor;
然后我称之为:
String[] testarg = {"Denver Bake"};
myDataBase.getReadableDatabase();
Cursor results = myDataBase.queryDB("SELECT * FROM Recipes WHERE r_name = ?", testarg);
我得到的错误是:
E/AndroidRuntime(30483): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dbtest/com.example.dbtest.MainActivity}: android.database.sqlite.SQLiteException: no such table: Recipes: , while compiling: SELECT * FROM Recipes WHERE r_name = ?
我知道Recipes表确实存在于我的数据库中,因为它出现在教程的SQLite数据库浏览器中。我已经尝试了教程评论中的一些建议,但我仍然遇到同样的错误,而且我现在还不确定要做什么。
谢谢你的到来。
这是我的创作
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
这是我的复制代码:
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
最后,我的资源文件夹中有数据库。错误消息告诉我数据库中的表不存在。
答案 0 :(得分:0)
您必须在浏览器中创建自己的SQLite数据库,并将此数据库复制到项目的assets文件夹中:
public class DbHelper extends SQLiteOpenHelper {
// The Android's default system path of your application database.
private static String PACKAGENAME = "com.SVLL";
private static String DB_PATH = "/data/data/" + PACKAGENAME + "/databases/";
private static String DB_NAME = "SVLL.sqlite";
private static final String TAG = "DbHelper";
private SQLiteDatabase myDataBase;
private final Context myContext;
/**
* Constructor Takes and keeps a reference of the passed context in order to
* access to the application assets and resources.
*
* @param context
*/
public DbHelper(final Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
/**
* Creates a empty database on the system and rewrites it with your own
* database.
* */
public final void createDataBase() throws IOException {
final boolean dbExist = checkDataBase();
SQLiteDatabase db_Read = null;
if (dbExist) {
// do nothing - database already exist
} else {
// By calling this method and empty database will be created into
// the default system path
// of your application so we are gonna be able to overwrite that
// database with our database.
// By calling this method and empty database will be created into
// the default system path
// of your application so we are gonna be able to overwrite that
// database with our database.
// db_Read = this.getReadableDatabase(DB_Internal);
db_Read = this.getReadableDatabase();
db_Read.close();
copyDataBase();
}
}
/**
* Restore whole database without any data
*
* @throws IOException
*/
public final void RestoreDatabase() throws IOException {
SQLiteDatabase db_Read = this.getReadableDatabase();
db_Read.close();
copyDataBase();
Log.i(TAG, "Database REstored");
}
/**
* Check if the database already exist to avoid re-copying the file each
* time you open the application.
*
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase() {
final File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}
/**
* Copies your database from your local assets-folder to the just created
* empty database in the system folder, from where it can be accessed and
* handled. This is done by transfering bytestream.
*
* @throws IOException
* */
private void copyDataBase() throws IOException {
// Open your local db as the input stream
final InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
final String outFileName = DB_PATH + DB_NAME;
// Open the empty db as the output stream
final OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
final byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public final SQLiteDatabase openDataBase() {
// Open the database
final String myPath = DB_PATH + DB_NAME;
if (myDataBase != null && myDataBase.isOpen()) {
myDataBase.close();
}
return myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
@Override
public final synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(final SQLiteDatabase arg0) {
}
@Override
public void onUpgrade(final SQLiteDatabase arg0, final int arg1,
final int arg2) {
}
}
答案 1 :(得分:0)
答案 2 :(得分:-1)
试试这个:
public class DBAdapter {
private static final String TAG = "[ DBAdapter ]";
private static final String KEY_TOU_ID = "tournamentid";
private static final String KEY_TEAM_ID = "teamid";
private static final String KEY_INNINGS_ID = "inningsid";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "YourDbName";
private static final String INNINGS_LIST_TABLE = "inningslist";
private static final String INNINGS_TABLE = "create table "
+ INNINGS_LIST_TABLE + "(" + KEY_INNINGS_ID
+ " integer primary key autoincrement," + KEY_GAME_ID + " integer,"
+ KEY_TEAM_ID + " integer," + KEY_TOU_ID + " integer);";
private DatabaseHelper DBhelper;
private SQLiteDatabase db;
private ProgressDialog pd;
public DBAdapter(Context ctx) {
DBhelper = new DatabaseHelper(ctx);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.i(TAG, "INNINGS TABLE" + INNINGS_LIST_TABLE);
db.execSQL(INNINGS_TABLE);
}
@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 titles");
onCreate(db);
}
}
public DBAdapter open() {
try {
db = DBhelper.getWritableDatabase();
} catch (SQLiteException sql) {
sql.printStackTrace();
}
return this;
}
public boolean isCreated() {
if (db != null) {
return db.isOpen();
}
return false;
}
public boolean isOpen() {
return db.isOpen();
}
public void close() {
DBhelper.close();
db.close();
}
}