Hello I was working with SQlite
on my phone with android studio
I have a simple database like this :
DATABASE 1 :
public class myDbAdapter {
myDbHelper myhelper;
public myDbAdapter(Context context) {
myhelper = new myDbHelper(context);
}
public long insertData(String name, String ip, String port, String rele) {
SQLiteDatabase dbb = myhelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(myDbHelper.NAME, name);
/* ... same for more items ...*/
long id = dbb.insert(TABLE_NAME, null, contentValues);
return id;
}
public String getData() {
SQLiteDatabase db = myhelper.getWritableDatabase();
String[] columns = {myDbHelper.UID, myDbHelper.NAME, myDbHelper.IP, myDbHelper.PORT, myDbHelper.RELE, myDbHelper.Hash};
Cursor cursor = db.query(TABLE_NAME, columns, null, null, null, null, null);
StringBuffer buffer = new StringBuffer();
int i = 0;
while (cursor.moveToNext()) {
i++;
int cid = cursor.getInt(cursor.getColumnIndex(myDbHelper.UID));
String name = cursor.getString(cursor.getColumnIndex(myDbHelper.NAME));
String ipE = cursor.getString(cursor.getColumnIndex(myDbHelper.IPE));
/* ... same for more items ...*/
buffer.append("*" + cid + "-" + name + "-" + ipE + "-" + port + "-" + rele + "
");
}
List1.colu = i;
return buffer.toString();
}
public int delete(String uid) {
SQLiteDatabase db = myhelper.getWritableDatabase();
String delgrp = "DELETE FROM " + TABLE_NAME + " WHERE _id='" + uid + "'";
db.execSQL(delgrp);
return 1;
}
static class myDbHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "myDatabase"; // Database Name
public static final String TABLE_NAME = "Data"; // Table Name
private static final int DATABASE_Version = 1; // Database Version
private static final String UID = "_id"; // Column I (Primary Key)
/* ... same for more items ...*/
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME +
" (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME + " VARCHAR(255) ," + IPE + " VARCHAR(255) ," + TEST1 + " VARCHAR(255) ," + TEST2 + " VARCHAR(255) ," + Hash + " VARCHAR(225));";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
private Context context;
public myDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_Version);
this.context = context;
}
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE);
} catch (Exception e) {
// Message.message(context,""+e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
// Message.message(context,"OnUpgrade");
db.execSQL(DROP_TABLE);
onCreate(db);
} catch (Exception e) {
// Message.message(context,""+e);
}
}
}
I Wanted to add another TABLE to same database (MyDataBase
)
So i created another java class named MyDbAdapter2
Same codes as above just changed class names and Table name
helper = new myDbAdapter(this);
helper2 = new myDbAdapter2(this);
DATABASE 2 :
public class myDbAdapter2 {
myDbHelper myhelper;
public myDbAdapter2(Context context) {
myhelper = new myDbHelper(context);
}
public long insertData(String name, String ip) {
/*...*/
}
public String getData() {
SQLiteDatabase db = myhelper.getWritableDatabase();
String[] columns = {myDbHelper.UID, myDbHelper.ITEM, myDbHelper.SUBITEM};
Cursor cursor = db.query(TABLE_NAME, columns, null, null, null, null, null);
StringBuffer buffer = new StringBuffer();
int i = 0;
while (cursor.moveToNext()) {
i++;
int cid = cursor.getInt(cursor.getColumnIndex(myDbHelper.UID));
String name = cursor.getString(cursor.getColumnIndex(myDbHelper.ITEM));
String ipe = cursor.getString(cursor.getColumnIndex(myDbHelper.SUBITEM));
buffer.append("*" + cid + "-" + name + "-" + ipe + "
");
}
// List1.colu=i;
return buffer.toString();
}
static class myDbHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "myDatabase"; // Database Name
public static final String TABLE_NAME = "Data2"; // Table Name
private static final int DATABASE_Version = 1; // Database Version
private static final String UID = "_id"; // Column I (Primary Key)
/*...*/ //Column II
// ... // Column III
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME +
" (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + ITEM + " VARCHAR(255) ," + SUBITEM + " VARCHAR(225));";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
private Context context;
public myDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_Version);
this.context = context;
}
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE);
} catch (Exception e) {
// Message.message(context,""+e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
// Message.message(context,"OnUpgrade");
db.execSQL(DROP_TABLE);
onCreate(db);
}catch (Exception e) {
// Message.message(context,""+e);
}
}
}
}
When i try to access the other (Data2
) database it cause a error !
android.database.sqlite.SQLiteException: no such table: Data2 (Sqlite code 1): , while compiling: SELECT _id, Item, SubItem FROM Data2, (OS error - 2:No such file or directory)
I Saw this on Log :
09-13 09:31:05.788 18454-18454/com.example.discopc.yubismart I/HwSecImmHelper: mSecurityInputMethodService is null
09-13 09:31:06.468 18454-18604/com.example.discopc.yubismart E/SQLiteLog: (1)
09-13 09:31:06.571 18454-18604/com.example.discopc.yubismart I/Process: Sending signal. PID: 18454 SIG: 9
Whats the problem ? First database works fine but second one not ,
Thanks.... ?
See Question&Answers more detail:
os