나는 안드로이드 응용 프로그램에서 내 자신의 데이터베이스를 사용하려고합니다. 이 항목에 대한 대부분의 모든 항목과 자습서에서는 SQLiteOpenHelper
을 사용하여 폴더를 만들고 열 수 있음을 제안합니다.이 폴더는 기본적으로 data/data/com.my.application/databases
폴더를 사용합니다. context.getAssets.open(dbname)
에서 기본 sqlite 경로로 파일을 복사하는 것이 좋습니다. 이것이 실제로 해결책 일 경우, 내 database.db
파일을 전달하는 올바른 경로는 무엇입니까?내 디렉토리에 안드로이드 데이터베이스가
1
A
답변
0
내 친구 당신은
/**
*
* Purpose: Open and Read database to fetch records from different database
* tables.
*
* @version 1.0
*
* @author ketan kalariya([email protected])
*
* **/
public class DataBaseHelper extends SQLiteOpenHelper {
private Context myContext;
private SQLiteDatabase myDataBase;
// Comman KEYs of tables
public static String MASTERFENCE_TABLE = "MasterFence";
public static String KEY_ID = "_id";
public static String AREA = "area";
public static String TAG = "tag";
public static String TITLE = "title";
public static String DESCR = "description";
public static String NO = "no";
public static String ADDFENCEPOINT_TABLE = "AddFencePoint";
public static String ADDLATLONG_TABLE = "AddLatLong";
public static String LAT = "lat";
public static String LONG = "long";
public static String TEMPFENCEPOINT_TABLE = "TempFencePoint";
public static String TEMPLATLONG_TABLE = "TempLatLong";
public DataBaseHelper(Context context) {
super(context, context.getResources().getString(R.string.dbname), null,
1);
this.myContext = context;
}
@Override
public void onCreate(SQLiteDatabase arg0) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
// ---Create the database---
public void createDataBase() throws IOException {
// ---Check whether database is already created or not---
boolean dbExist = checkDataBase();
if (!dbExist) {
this.getReadableDatabase();
try {
// ---If not created then copy the database---
copyDataBase();
this.close();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
// --- Check whether database already created or not---
private boolean checkDataBase() {
try {
final String myPath = "/data/data/" + myContext.getPackageName()
+ "/databases/" + myContext.getString(R.string.dbname);
final File f = new File(myPath);
if (f.exists())
return true;
else
return false;
} catch (SQLiteException e) {
e.printStackTrace();
return false;
}
}
// --- Copy the database to the output stream---
public void copyDataBase() throws IOException {
final InputStream myInput = myContext.getAssets().open(
myContext.getString(R.string.dbname));
final String outFileName = "/data/data/" + myContext.getPackageName()
+ "/databases/" + myContext.getString(R.string.dbname);
final OutputStream myOutput = new FileOutputStream(outFileName);
final byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
// --- Open the database---
final String myPath = "/data/data/" + myContext.getPackageName()
+ "/databases/" + myContext.getString(R.string.dbname);
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
/**
* Close the database
*/
public synchronized void closeDatabase() {
if (myDataBase != null)
;
myDataBase.close();
SQLiteDatabase.releaseMemory();
super.close();
}
/** This method is insert require detail in database table. **/
public long insertMasterFence(String area, String title, String descr,
String tag) {
ContentValues values = new ContentValues();
values.put(AREA, area);
values.put(TITLE, title);
values.put(DESCR, descr);
values.put(TAG, tag);
Log.v(" db ", area + " " + title + " " + " " + descr + " " + tag);
return myDataBase.insert(MASTERFENCE_TABLE, null, values);
}
+0
. 올바르게 사용하면 제대로 작동하지만, 많은 에러를 줄 수 있습니다. 왜냐하면'.getWritableDatabase'를 통해 db를 얻으면 빈 데이터베이스를 줄 수 있기 때문입니다 (onCreate에 코드가 없으므로 – zapl
관련 문제
- 1. 안드로이드 애플 리케이션에 데이터베이스가
- 2. 태블릿에서 안드로이드 데이터베이스가 읽히지 않습니다.
- 3. 데이터베이스가 채워지지 않으면 내 안드로이드 응용 프로그램이 분쇄되고 있습니다.
- 4. 내 프로젝트 디렉토리에 cmd를
- 5. 내 디렉토리에 UnauthorizedAccessException이 있습니다.
- 6. 데이터베이스가 실제 안드로이드 장치의 모든 것을 재설정합니다.
- 7. 안드로이드 앱을 제거하면 sdcard에 저장된 데이터베이스가 삭제됩니까?
- 8. 안드로이드/데이터/데이터/(packageName)/데이터베이스가 생성되지 않았습니다.
- 9. 내 프로젝트 디렉토리에 .gitignore 파일이
- 10. 내 디렉토리에 이런 일이 PHP
- 11. 내 에뮬레이터에서 작동하는 SQLite 데이터베이스가 내 장치에서 작동하지 않습니다.
- 12. 데이터베이스가
- 13. 안드로이드 소스 코드를 다른 디렉토리에 복제하기
- 14. 내 데이터베이스가 삼각형 관계를 가질 수 있습니까?
- 15. 내 (데이터베이스가 아닌) fetch_object 함수 만들기
- 16. 내 ios sqlite3 데이터베이스가 생성되지 않습니다.
- 17. 안드로이드 : 프로그램 내 안드로이드 활동에
- 18. 내 디렉토리에 다음 파일이이 순간 PHP
- 19. PEAR이 내 사용자 디렉토리에 설치되는 이유는 무엇입니까?
- 20. 내 문서 디렉토리에 NSMutableArray 쓰기가 실패합니다.
- 21. Heroku gem을 내 루트 디렉토리에 설치
- 22. 내 파일을 Android 디렉토리에 작성하는 방법
- 23. 서브 디렉토리에 내 webservice를 넣을 수 있습니까?
- 24. 안드로이드 삽입 데이터 SQLite 오류 java.lang.IllegalStateException에 의해 발생 : 데이터베이스가 열려
- 25. 디렉토리에
- 26. 라우터없이 스마트 폰에서 내 latops XAMPP 디렉토리에 액세스
- 27. 내 작업 디렉토리에 직접 파일을 다운로드하고 싶은 내 작업 디렉토리
- 28. 내 DirectoryEntry가 실제로 내 LDAP 디렉토리에 연결되어 있는지 확인하는 방법?
- 29. 내 홈 페이지 트래픽을 내 레일 서브 디렉토리에 연결하는 방법
- 30. 안드로이드 - 데이터베이스가 데이터베이스 파일을 잃어 버렸을 때 데이터베이스를 지 웁니다.
그래서 당신이 어떤 경로를 알고 싶어이 하나 개의 코드를 시도 할 수 있습니다? 목적지? –
예. 그렇습니다. 프로젝트의 "assets"폴더에 데이터베이스 파일을 넣어야한다고 생각했습니다. 여러분이'onCreate' /'onUpgrade' 메카니즘을 사용하지 않으면'extends SQLiteOpenHelper' 부분을 삭제하십시오. – cgalvao1993