Reputation: 1289
First i created one table, which worked. The second one i did it in the same fashion, still it gives error: no such table my_contacts.
So this is the code:
[in the main activity]
DBOperations.insert(this, TABLE_MY_CONTACTS, fields, vals);
Where DBOperations class looks like this:
public class DBOperations implements Tables {
// inserting 1 contact into a table
public static boolean insert (Context context, String table, ArrayList <String> fields, ArrayList <String> vals) {
if (fields.size() != vals.size())
return false;
MySQLite_methods mysql = new MySQLite_methods (context, table); // creates the table!!!!!!!!!
ContentValues values = new ContentValues();
for (int i=0; i<fields.size(); i++)
values.put(fields.get(i), vals.get(i));
// checking if the card for that ulink already existed (if so, overwrite)
String[] COLUMNS = {ULINK};
SQLiteDatabase dbr = mysql.getReadableDatabase();
Cursor cursor = dbr.query(table, COLUMNS, null, null, null, null, null);
SQLiteDatabase dbw = mysql.getWritableDatabase();
if (cursor == null || cursor.moveToFirst() == false)
dbw.insertOrThrow(table, null, values);
else
if (cursor.moveToFirst()) {
String value_ulink = cursor.getString(0);
Log.d("geo", "val="+value_ulink);
dbw.update(table, values, ULINK+"='"+value_ulink+"'", null);
}
return true;
}
// ....
}
MySQLite_methods class extends SQLiteOpenHelper and looks like this:
public class MySQLite_methods extends SQLiteOpenHelper implements Tables {
private static final String DATABASE_NAME = "my_database.db" ;
private static final int DATABASE_VERSION = 1;
private String TABLE_NAME;
/** Create a helper object for the Events database */
public MySQLite_methods (Context ctx, String table_name)
{
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
this.TABLE_NAME = table_name;
}
@Override
public void onCreate(SQLiteDatabase db)
{
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME +
" (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
NAME + " TEXT NOT NULL," +
PHONE + " TEXT NOT NULL,"+
EMAIL + " TEXT NOT NULL,"+
FACEBOOK + " TEXT,"+
WEBSITE + " TEXT,"+
MAJOR + " TEXT NOT NULL,"+
ULINK + " TEXT NOT NULL" + ");" ;
Log.d("geo", sql);
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
And finally the interface Tables looks like this:
public interface Tables extends BaseColumns {
/*
* From the BaseColumns interface we inherit the constants:
* _ID - The unique ID for a row.
* _COUNT - The count of rows in a directory.
*/
//tables name
public static final String TABLE_MY_CARD= "my_card" ;
public static final String TABLE_MY_CONTACTS = "my_contacts" ;
// Columns
public static final String NAME = "name" ;
public static final String PHONE = "phone" ;
public static final String EMAIL = "email";
public static final String FACEBOOK = "facebook";
public static final String WEBSITE = "web";
public static final String MAJOR = "major";
public static final String ULINK = "ulink";
}
With the same steps I did create another table which was successful. But this one i can't create at all!
Upvotes: 0
Views: 429
Reputation: 2657
I think onUpgrade and onCreate are not executing the way you think they are.
onCreate will only execute when the DB file needs to be created.
onUpgrade on executes as necessary (i.e. newVersion > oldVersion)
I believe what is happening is that the first table gets created because onCreate executes, since the DB does not exist yet. Then when you try to create the second table it doesn't work because neither onCreate or onUpgrade is called.
You should create all your tables during onCreate and handle any upgrade logic required during onUpgrade.
Upvotes: 2