(۱۳۹۴/۱۱/۲۰, ۰۶:۰۹ ب.ظ)msh نوشته: با سلام
اینکه db در داخل برنامه بسازم و بهش دسترسی داشته باشم و بلدم.
مشکل من این هست که یک سری اطلاعات در قالب جدول در SqLite دارم با پسوند SqLite .
چطور اونها رو در برنامه استفاده کنم؟
این کدیست که من استفاده کردم اما از حلقه Try خارج میشه و پیام Could not open database میده.
کد پیاچپی:String DB_PATH = "/data/data/my_package_name/databases/";
String DB_NAME = "DB1.sqlite";
SQLiteDatabase checkDB = null;
try
{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
if (checkDB.equals(null)) ;
else
Toast.makeText(getApplicationContext(), "DB Opened...", Toast.LENGTH_LONG).show();
} catch (Exception e)
{
Toast.makeText(getApplicationContext(), e+"\ndatabase does't exist yet...", Toast.LENGTH_LONG).show();
}
نکته مهم اینکه در اندروید استدیو به طور پیش فرض پوشه Assets وجود نداره و من خودم ایجادش کردم و db1.sqlite رو توش قرار دادم.
ممنون میشم راهنمایی بفرمایین.
سلام
اول یه کلاس مثل زیر بساز:
کد پیاچپی:
package com.sinabelle.marriagepsychology;
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 DatabaseConnecter extends SQLiteOpenHelper {
String DB_PATH = null;
public static String DB_NAME = "mydb";
private SQLiteDatabase myDataBase;
private final Context myContext;
/**
* Constructor Takes and keeps a reference of the passed context in order to
* access to the application assets and resources.
*
* @param context
*/
public DatabaseConnecter(Context context) {
super(context, DB_NAME, null, 2);
this.myContext = context;
DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
}
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();
this.getWritableDatabase();
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_READWRITE);
} catch (SQLiteException e) {
// database does\'t exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* 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 transfering bytestream.
* */
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
// 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;
// SQLiteDatabase.NO_LOCALIZED_COLLATORS
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE
| SQLiteDatabase.NO_LOCALIZED_COLLATORS
| SQLiteDatabase.CREATE_IF_NECESSARY);
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
// return cursor
public Cursor query(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having,
String orderBy) {
return myDataBase.query("tbl_mp", null, null, null, null, null,
null);
}
@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
}
}
بعد اینجوری ازش استفاده کن:
کد پیاچپی:
Load_Database();
db = myDbHelper.getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM tbl_mp where WitchPart = 1", null);
data = new ArrayList<HashMap<String, String>>();
for (; c.moveToNext() ; ) {
HashMap<String, String> map = new HashMap<String, String>();
id = c.getString(c.getColumnIndex("Id"));
String name = c.getString(c.getColumnIndex("Topic"));
map.put("id", id);
map.put("name", name);
data.add(map);
}
adapter = new list_view(this, data);
lv.setAdapter(adapter);
private void Load_Database() throws Error {
myDbHelper = new DatabaseConnecter(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("ناتوان در ساخت پایگاه داده.");
}
try {
myDbHelper.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
}
اینا کدهای برنامه خودم بودن دیگه شما طبق مصرفت تغییرشون بده.
همین....!!