ورود

View Full Version : اتصال به پایگاه داده در اندروید



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/";
و میگه این مسیر وجود نداره
لطفا راهنمایی کنید

nargesjooon
چهارشنبه 25 آذر 1394, 20:34 عصر
منم مشکل تو رو دارممممممممم :ناراحت:

hamedg1366
چهارشنبه 25 آذر 1394, 21:01 عصر
شما هم میتونین از دیتابیس خارجی استفاده کنید تا دن و فنگ SQLiteOpenHelper رو نداشته باشید

هم اینکه با یه جستجوی ساده توی همین انجمن به تایپیک دوست خوبم آقای poorman را ببینید که از سیر تا پیاز دیتابیس اندروید رو توضیح و آموزش داده ، پیدا نکردین بگین لینکش رو بذارم براتون

nargesjooon
شنبه 05 دی 1394, 11:35 صبح
سلام دوستان من یک برنامه کتاب نوشتم و این برنامه قراره به این پایگاه داده وصل بشه و اطلاعات رو از اون بخونه پایگاه داده در 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/";
و میگه این مسیر وجود نداره
لطفا راهنمایی کنید
دوست عزیز شما اول باید از فایل کتابخانه ایSQLiteAssetHelper استفاده کنی تا مشکلت حل بشه
در ضمن از متند mydbs =DataBase.getReadableDatabase(); قافل نشو ک برای خوندن دیتابیس هستش
اگ متوجه منظورم نشدی بگو تا سورس کد بزارم تا واضح تر منظورمو بفهمی :لبخند:

hafez1
چهارشنبه 23 دی 1394, 19:10 عصر
سلام دوستان.
من این کدی که گذاشتید رو داخل یه کلاس پایگاه داده قرار دادم.
بدش میخاسم توی یه ادیت تکست مقادیری که از قبل توی جدولم هستو نشونش بدم.
فقز برای اینکه امتحان کنم کار میکنه یا نه.
اومدم یه شی از کلاس پایگاه داده ساختم و بدش متد show رو صدا زدم.
به این صورت:

138372
حالا اونجایی که دارم شی رو میسازم ورودی تابع باید یه context باشه.
من نمیدونسم چی بذارم الکی گذاشتم this.
و برنامه اجرا نمیشه.
ورودی چی بهش بدم؟؟؟؟؟؟؟؟