在我的应用程序中我使用使用现有的sqlite数据库,我需要列出数据库中表的所有记录。我有六张桌子。
我的sqlite数据库文件名是: festival.sqlite ,存储在我的资源文件夹中。
用于复制并列出我使用以下代码的记录。但是在运行此代码时我没有得到这样的表异常。我知道在sqlite文件中有表。
如何从表中获取记录。请帮我。 我的代码:
public class main extends SQLiteOpenHelper {
//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.sql.test/databases/";
private static String DB_NAME = "festival";
private SQLiteDatabase myDataBase;
private final Context myContext;
public main(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
Log.e("database", "database exist");
}else{
Log.e("database","not exist");
this.getReadableDatabase().close();
try {
copyDataBase();
} catch (IOException e) {
//throw new Error("Error copying database");
Log.e("exception-1",e.getMessage());
Log.e("exception-1", "Error copying database");
}
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[618496];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
myDataBase.setVersion(1);
myDataBase.setLocale(Locale.getDefault());
myDataBase.setLockingEnabled(true);
try{
Cursor cx = myDataBase.rawQuery("SELECT * FROM events" , null);
if (cx != null ) {
if (cx.moveToFirst()) {
do {
Log.e("ID",String.valueOf(cx.getInt(0)));
}while (cx.moveToNext());
}
}
myDataBase.close();
}
catch(Exception e){
Log.e("exception-table", e.getMessage());
}
}
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
在另一项活动中,我称之为以下方法:
main myDbHelper = new main(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
Log.e("ex-2",ioe.getMessage());
// throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
// throw sqle;
Log.e("exception",sqle.getMessage());
}
我的日志猫:
07-25 10:33:30.748: ERROR/Database(823): sqlite3_open_v2("/data/data/com.sql.test/databases/festival", &handle, 1, NULL) failed
07-25 10:33:30.748: ERROR/database(823): not exist
07-25 10:33:30.917: ERROR/exception-1(823): festival
07-25 10:33:30.917: ERROR/exception-1(823): Error copying database
07-25 10:33:30.998: ERROR/exception-table(823): no such table: events: , while compiling: SELECT * FROM events
答案 0 :(得分:0)
如果你还没有添加android_metadata表,那么只需添加它并编译即可。
有关更多信息,这是非常好的教程
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
答案 1 :(得分:0)
请参阅以下链接以从资产中复制数据库。还要检查带有case sencitive的表名