mobarra750
چهارشنبه 25 آذر 1394, 14:21 عصر
سلام دوستان من یک برنامه کتاب نوشتم و این برنامه قراره به این پایگاه داده وصل بشه و اطلاعات رو از اون بخونه پایگاه داده در sqlite طراحی شده
public class database extends SQLiteOpenHelper {
// Database Path
private static String DB_PATH = "/data/data/com.mohamad.book/databases/";
private static String DB_NAME = "book";
private SQLiteDatabase myDataBase = null;
private final Context myContext;
// Database Version
private static final int DATABASE_VERSION = 1;
public database(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
this.myContext=context;
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDatabase() throws IOException{
boolean dbexist = checkDatabase();
if(dbexist)
{
//System.out.println(" Database exists.");
}
else{
this.getReadableDatabase();
try{
copyDatabase();
}
catch(IOException e){
throw new Error("Error copying database");
}
}
}
/**
* 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
*/
public boolean checkDatabase() {
//SQLiteDatabase checkdb = null;
boolean checkdb = false;
try{
String myPath = DB_PATH + DB_NAME;
File dbfile = new File(myPath);
//checkdb = SQLiteDatabase.openDatabase(myPath,null,SQLiteData base.OPEN_READWRITE);
checkdb = dbfile.exists();
}
catch(SQLiteException e){
System.out.println("Database doesn't exist");
}
return checkdb;
}
/**
* 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 transferring by stream.
* */
private void copyDatabase() throws IOException {
//Open your local db as the input stream
InputStream myinput = myContext.getAssets().open(DB_NAME);
//Open the empty db as the output stream
OutputStream myoutput = new FileOutputStream("/data/data/com.mohamad.book/databases/book");
// transfer byte to inputfile to 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 open() throws SQLException
{
//Open the database
String mypath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close(){
if(myDataBase != null){
myDataBase.close();
}
super.close();
}
public String show(int row,int filed,String table){
Cursor cursor = myDataBase.rawQuery("SELECT * FROM "+table, null);
cursor.moveToPosition(row);
String str=cursor.getString(filed);
return str;
}
ولی موقع اجرا به پایگاه داده وصل نمیشه در ضمن برنامه از این خط alarm میگیره
private static String DB_PATH = "/data/data/com.mohamad.book/databases/";
و میگه این مسیر وجود نداره
لطفا راهنمایی کنید
public class database extends SQLiteOpenHelper {
// Database Path
private static String DB_PATH = "/data/data/com.mohamad.book/databases/";
private static String DB_NAME = "book";
private SQLiteDatabase myDataBase = null;
private final Context myContext;
// Database Version
private static final int DATABASE_VERSION = 1;
public database(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
this.myContext=context;
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDatabase() throws IOException{
boolean dbexist = checkDatabase();
if(dbexist)
{
//System.out.println(" Database exists.");
}
else{
this.getReadableDatabase();
try{
copyDatabase();
}
catch(IOException e){
throw new Error("Error copying database");
}
}
}
/**
* 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
*/
public boolean checkDatabase() {
//SQLiteDatabase checkdb = null;
boolean checkdb = false;
try{
String myPath = DB_PATH + DB_NAME;
File dbfile = new File(myPath);
//checkdb = SQLiteDatabase.openDatabase(myPath,null,SQLiteData base.OPEN_READWRITE);
checkdb = dbfile.exists();
}
catch(SQLiteException e){
System.out.println("Database doesn't exist");
}
return checkdb;
}
/**
* 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 transferring by stream.
* */
private void copyDatabase() throws IOException {
//Open your local db as the input stream
InputStream myinput = myContext.getAssets().open(DB_NAME);
//Open the empty db as the output stream
OutputStream myoutput = new FileOutputStream("/data/data/com.mohamad.book/databases/book");
// transfer byte to inputfile to 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 open() throws SQLException
{
//Open the database
String mypath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close(){
if(myDataBase != null){
myDataBase.close();
}
super.close();
}
public String show(int row,int filed,String table){
Cursor cursor = myDataBase.rawQuery("SELECT * FROM "+table, null);
cursor.moveToPosition(row);
String str=cursor.getString(filed);
return str;
}
ولی موقع اجرا به پایگاه داده وصل نمیشه در ضمن برنامه از این خط alarm میگیره
private static String DB_PATH = "/data/data/com.mohamad.book/databases/";
و میگه این مسیر وجود نداره
لطفا راهنمایی کنید