내 안드로이드 응용 프로그램에 큰 문제가 있습니다. 나는 처음으로 sqlite 데이터베이스로 안드로이드 응용 프로그램을 개발하고 있지만 문제는 해결할 수 없다.Android Eclipse 프로젝트의 자산 폴더에있는 데이터베이스
나는 이름 saldb.sqlite 이클립스 프로젝트의 자산 폴더에 내 SQLite 데이터베이스를
나는 Singletone 패턴으로 데이터베이스를 관리 할 수있는 다음과 같은 클래스가 :
package sal.app.logic;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseManager extends SQLiteOpenHelper{
private static DataBaseManager dbManagerInstance = null;
private Context salContext;
private SQLiteDatabase salDatabase;
private static String DB_PATH = "/data/data/sal.app/databases/";
private static String DB_NAME = "saldb.sqlite";
private DataBaseManager(Context c)
{
super(c, DB_NAME, null, 1);
//this.salContext = c;
}
public static DataBaseManager getSalDatabase(Context c)
{
if (dbManagerInstance == null)
{
dbManagerInstance = new DataBaseManager(c);
}
return dbManagerInstance;
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
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.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("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){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = salContext.getAssets().open(DB_NAME);
// Path to the just created empty db
//String outFileName = DB_PATH + DB_NAME;
String outFileName = "/data/data/sal.app/databases/saldb.sqlite";
//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 void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
salDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if(salDatabase != null)
salDatabase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
//db.execSQL("Insert Into Question(_id,level,text,idTopic) Values (1,1,'asa',0)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
// Add your public helper methods to access and get content from the database.
// You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
// to you to create adapters for your views.
public Question getOneQuestion()
{
//list of Versioni, search result with query text
Question quest = new Question();
try
{
//open database to query
openDataBase();
//salDatabase.execSQL("Insert Into Question(_id,level,text,idTopic) Values (1,1,'asa',0)");
//Cursor cursor = salDatabase.rawQuery("SELECT text, idTopic, level from Question WHERE level=2", null);
Cursor cursor = salDatabase.rawQuery("SELECT * from Question", null);
/*Cursor cursor = salDatabase.query("Question",
new String[] { "text","idTopic","level"},
"level=2",
null ,
null,
null,
"RANDOM() LIMIT 1");*/
//Cursor c = db.rawQuery(select, null); */
//mapped all rows to data object
if (cursor.moveToFirst())
{
System.out.println(cursor.getString(2));
do
{
Cursor cursor2 = salDatabase.rawQuery("SELECT * from Topic WHERE _id=0", null);
cursor2.moveToFirst();
Topic t = new Topic(cursor2.getString(1));
quest = new Question(cursor.getString(2),t,(int)cursor.getShort(1));
break;
} while (cursor.moveToNext());
}
//close cursor
cursor.close();
}
catch(Exception ex)
{
System.out.println("DatabaseHelper.search()- : ex " + ex.getClass() +", "+ ex.getMessage());
}
//
return quest;
}
/*public ArrayList<Answer> getAnswersOfQuestion(Questin q)
{
}*/
}
하지만 난 실행 처음에이 내 응용 프로그램 내가 해제 한 다음하는 오류 : 55 :
05-29 23 45.684 : D/DDM 힙 (221) :있어 기능 목록 요청 05-29 23 : 55 : 46.295 : D/dalvikvm (221) : GC 해제 된 519 개체/45792 바이트 109ms 05-29 23 : 55 : 46.544 : E/데이터베이스 (221) : sqlite3_open_v2 ("/ 데이터/데이터/sal.app/데이터베이스/saldb.sqlite", & 핸들, 1, NULL)은
05- 실패 29 : 55 : 46.594 : D/AndroidRuntime (221) : VM 종료 중 05-29 23 : 55 : 46.604 : dalvikvm (221) : threadid = 3 : catch되지 않은 예외 (그룹 = 0x4001b188)로 스레드 종료
05-29 23 : 55 : 46.604 : E는/AndroidRuntime는 (221) : catch되지 않은 핸들러 : 인해 캐치되지 않는 예외에 종료 주 스레드
내 주요 활동 메신저에서이 일 :
0,123,225,663,126,482,791,627,584 난, 오류가 데이터베이스를 발생 해달라고 응용 프로그램을 실행 두 번째에서53,210 올바른 경로에 있지만 테이블 android_metadata
이나는 또한 말할 수 내가/데이터/데이터의 올바른 데이터베이스를 세우면 그 /sal.app/databases/ 전체 프로그램이 작동합니다 ... 오류는 copyDatabase에 있습니다.
http://stackoverflow.com/questions/9109438/using-already-created-database-with-android/9109728#9109728 –