میشه یکی جواب بده (مشکل در خواندن دیتابیس)
سلام من یه برنامه نوشتن با یه دیتابیس ازبرنامه SQLite expert براش یه دکمه گذاشتم که وقتی روش کلیک بشه دیتابیس رو نشون بده ولی وقتی روش کلیلک میکنم فورس کلوز میکنه هرچی هم میگردم نمیتونم مشکلش رو پیدا کنم .
public class ActivityDatabase extends SQLiteOpenHelper {
public final String path = "data/data/com.elham.app.database/databases/";
public final String Name = "database";
public SQLiteDatabase mydb;
private final Context mycontext;
public ActivityDatabase(Context context) {
super(context, "database", null, 1);
mycontext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void useable() {
boolean checkdb = checkdb();
if (checkdb) {
} else {
this.getReadableDatabase();
try {
copydatabase();
}
catch (IOException e) {
}
}
}
public void open() {
mydb = SQLiteDatabase.openDatabase(path + Name, null, SQLiteDatabase.OPEN_READWRITE);
}
public void close() {
mydb.close();
}
public boolean checkdb() {
SQLiteDatabase db = null;
try {
db = SQLiteDatabase.openDatabase(path + Name, null, SQLiteDatabase.OPEN_READONLY);
}
catch (SQLException e) {}
return db != null ? true : false;
}
public void copydatabase() throws IOException {
OutputStream myOutput = new FileOutputStream(path + Name);
byte[] buffer = new byte[1024];
int length;
InputStream myInput = mycontext.getAssets().open(Name);
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myInput.close();
myOutput.flush();
myOutput.close();
}
public String Display(int row, int field, String table) {
Cursor cu = mydb.rawQuery("select *from " + table, null);
cu.moveToPosition(row);
String s = cu.getString(field);
return s;
}
}
اینم کد
public class ActivityDatabase extends SQLiteOpenHelper {
public final String path = "data/data/com.elham.app.chocolate/databases/";
public final String Name = "database";
public SQLiteDatabase mydb;
private final Context mycontext;
public ActivityDatabase(Context context) {
super(context, "database", null, 1);
mycontext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void useable() {
boolean checkdb = checkdb();
if (checkdb) {
} else {
this.getReadableDatabase();
try {
copydatabase();
}
catch (IOException e) {
}
}
}
public void open() {
mydb = SQLiteDatabase.openDatabase(path + Name, null, SQLiteDatabase.OPEN_READWRITE);
}
public void close() {
mydb.close();
}
public boolean checkdb() {
SQLiteDatabase db = null;
try {
db = SQLiteDatabase.openDatabase(path + Name, null, SQLiteDatabase.OPEN_READONLY);
}
catch (SQLException e) {}
return db != null ? true : false;
}
public void copydatabase() throws IOException {
OutputStream myOutput = new FileOutputStream(path + Name);
byte[] buffer = new byte[1024];
int length;
InputStream myInput = mycontext.getAssets().open(Name);
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myInput.close();
myOutput.flush();
myOutput.close();
}
public String Display(int row, int field, String table) {
Cursor cu = mydb.rawQuery("select * from" + table, null);
cu.moveToPosition(row);
String s = cu.getString(field);
return s;
}
}
اینم دستورات اجرا
public class ChocolateActivity extends Activity {
private ActivityDatabase db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.button1);
db = new ActivityDatabase(this);
db.useable();
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
db.open();
String s = db.Display(0, 1, "chocolate");
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
db.close();
}
});
}
}
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .