SQLite数据库查询Android

时间:2014-05-14 03:58:52

标签: android sqlite

我在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();
}

最后,我的资源文件夹中有数据库。错误消息告诉我数据库中的表不存在。

3 个答案:

答案 0 :(得分:0)

您必须在浏览器中创建自己的SQLite数据库,并将此数据库复制到项目的assets文件夹中:

  1. 在浏览器中创建数据库。
  2. 复制数据库
  3. 粘贴到您的资源文件夹中。
  4. 创建一些扩展SQLiteOpenHelper类的dbhelper类,并将数据库复制出assets文件夹,以便应用程序可以访问它:
  5. 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)

CL非常正确。我的副本代码是正确的,它没有被正确调用。我最后做的是草率但我从我的查询代码而不是我的创建数据库调用copyDataBase()。我知道愚蠢和低效,但它现在有效。

答案 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();
}

}