SQLite数据库查询Android
我在Android应用程序上查询SQLite数据库时遇到问题。我曾经设置过我的数据库助手类 我做了一个简单的查询函数:SQLite数据库查询Android,android,sqlite,Android,Sqlite,我在Android应用程序上查询SQLite数据库时遇到问题。我曾经设置过我的数据库助手类 我做了一个简单的查询函数: public Cursor queryDB(String Query,String[] args){ Cursor cursor = myDataBase.rawQuery(Query, args); return cursor; 然后我这样称呼它: String[] testarg = {"Denver Bake"}; myDataBase.getReadabl
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();
}
最后,我的资产文件夹中有我的数据库。错误消息告诉我数据库中的表不存在。请尝试以下操作:
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();
}
}您必须在浏览器中创建自己的SQLite数据库,并将此数据库复制到项目的资产文件夹中:
public类DbHelper扩展了SQLiteOpenHelper{
//应用程序数据库的Android默认系统路径。
私有静态字符串PACKAGENAME=“com.SVLL”;
私有静态字符串DB_PATH=“/data/data/”+PACKAGENAME+“/databases/”;
私有静态字符串DB_NAME=“SVLL.sqlite”;
私有静态最终字符串TAG=“DbHelper”;
私有SQLiteDatabase-myDataBase;
私有最终上下文myContext;
/**
*构造函数获取并保留所传递上下文的引用,以便
*访问应用程序资产和资源。
*
*@param上下文
*/
公共DbHelper(最终上下文){
super(上下文,DB_名称,null,1);
this.myContext=上下文;
}
/**
*在系统上创建一个空数据库,并用自己的数据库重写它
*数据库。
* */
public final void createDataBase()引发IOException{
最终布尔值dbExist=checkDataBase();
SQLiteDatabase db_Read=null;
if(dbExist){
//不执行任何操作-数据库已存在
}否则{
//通过调用此方法,空数据库将被创建到
//默认系统路径
//你的应用程序,所以我们可以覆盖它
//数据库与我们的数据库。
//通过调用此方法,空数据库将被创建到
//默认系统路径
//你的应用程序,所以我们可以覆盖它
//数据库与我们的数据库。
//db_Read=this.getReadableDatabase(db_内部);
db_Read=this.getReadableDatabase();
db_Read.close();
copyDataBase();
}
}
/**
*无需任何数据即可恢复整个数据库
*
*@抛出异常
*/
public final void RestoreDatabase()引发IOException{
SQLiteDatabase db_Read=this.getReadableDatabase();
db_Read.close();
copyDataBase();
Log.i(标记“数据库已恢复”);
}
/**
*检查数据库是否已存在,以避免每次重新复制文件
*打开应用程序的时间。
*
*@如果存在则返回true,如果不存在则返回false
*/
私有布尔校验数据库(){
最终文件dbFile=新文件(DB_路径+DB_名称);
返回dbFile.exists();
}
/**
*将数据库从本地资产文件夹复制到刚创建的
*系统文件夹中的空数据库,从中可以访问和
*已处理。这是通过传输ByTestStream来完成的。
*
*@抛出异常
* */
私有void copyDataBase()引发IOException{
//打开本地数据库作为输入流
final InputStream myInput=myContext.getAssets().open(DB_名称);
//刚创建的空数据库的路径
最终字符串outFileName=DB_路径+DB_名称;
//打开空数据库作为输出流
final OutputStream myOutput=新文件OutputStream(outFileName);
//将字节从输入文件传输到输出文件
最终字节[]缓冲区=新字节[1024];
整数长度;
而((长度=myInput.read(缓冲区))>0){
写入(缓冲区,0,长度);
}
//关闭溪流
myOutput.flush();
myOutput.close();
myInput.close();
}
公共最终SQLiteDatabase openDataBase(){
//打开数据库
最后一个字符串myPath=DB_PATH+DB_NAME;
if(myDataBase!=null&&myDataBase.isOpen()){
myDataBase.close();
}
返回myDataBase=SQLiteDatabase.openDatabase(myPath,null,
SQLiteDatabase.OPEN_READWRITE);
}
@凌驾
公共最终同步作废关闭(){
if(myDataBase!=null)
myDataBase.close();
super.close();
}
@凌驾
创建公共void(最终SQLiteDatabase arg0){
}
@凌驾
public void onUpgrade(最终SQLiteDatabase arg0,最终int arg1,
最终整数(arg2){
}
}
CL说得很对。我的复制代码是正确的,它没有被正确调用。最后我做的是草率的,但我从查询代码中调用了copyDataBase(),而不是从create database中调用。我知道这是愚蠢和低效的,但它现在起作用了 发布创建表查询。您必须在浏览器中创建自己的sqlite数据库。然后将此数据库复制到项目的“资产”文件夹中。请阅读错误消息!正如它(和@dipali)所说,您没有创建数据库。下面来自@krishanDhamat的答案显示了您应该做什么:使用SQLiteOpenHelper。您需要阅读其他地方的内容,以了解为什么您没有正确复制数据库。显示从资产文件夹复制数据库的代码