Reputation: 75
The code I posted below is what I used for my Database in the android application. My program is currently still on my computer and running with the emulator. My question is how will this whole database thing work ones someone downloads my program off of google play? Will they have their own DATABASE_NAME and DATABASE_TABLE one their phone? In which case I wont have to worry about anything. Or will the DATABASE_NAME and DATABASE_TABLE need to be different for every used that downloads the program and uses the Database on the app? In this case I'm not sure how I would taylor the Database for each individual user. Any help is appreciated. Thanks...
public class DBAdapter {
private static final String TAG = "DBAdapter"; //used for logging database version changes
// Field Names:
public static final String KEY_ROWID = "_id";
public static final String KEY_DE = "de";
public static final String KEY_MO = "mo";
public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_DESTINATION, KEY_MONTH};
// DataBase info:
public static final String DATABASE_NAME = "Prog.db";
public static final String DATABASE_TABLE = "prog_table";
//IF ANY CHANGES ARE DONE HERE THE VERSION NEEDS TO BE INCREMENTED OTHERWISE THE PROGRAM WILL CRASH
public static final int DATABASE_VERSION = 1; // The version number must be incremented each time a change to DB structure occurs.
//SQL statement to create database
private static final String DATABASE_CREATE_SQL =
"CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DE + " TEXT NOT NULL, " + KEY_MO + " TEXT " ");";
private final Context context;
private DatabaseHelper myDBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx) {
this.context = ctx;
//initializing the DatabaseHelper
myDBHelper = new DatabaseHelper(context);
}
// Open the database connection.
public DBAdapter open() {
db = myDBHelper.getWritableDatabase();
return this;
}
// Close the database connection.
public void close() {
myDBHelper.close();
}
// Add a new set of values to be inserted into the database.
public long insertData(String de, String mo) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_DE, de);
initialValues.put(KEY_MO, mo);
// Insert the data into the database.
return db.insert(DATABASE_TABLE, null, initialValues);
}
// Delete a row from the database, by rowId (primary key)
public boolean deleteRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
return db.delete(DATABASE_TABLE, where, null) != 0;
}
public void deleteAll() {
Cursor c = getAllRows();
long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
if (c.moveToFirst()) {
do {
deleteRow(c.getLong((int) rowId));
} while (c.moveToNext());
}
c.close();
}
// Return all data in the database.
public Cursor getAllRows() {
String where = null;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
// Get a specific row (by rowId)
public Cursor getRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
private static class DatabaseHelper extends SQLiteOpenHelper{
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(DATABASE_CREATE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading application's database from version " + oldVersion
+ " to " + newVersion + ", which will destroy all old data!");
// Destroy old database:
_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
// Recreate new database:
onCreate(_db);
}
}
}
Upvotes: 0
Views: 99
Reputation: 1
sqlite database
class Mydatabase(
var page: Context,
) : SQLiteOpenHelper(page, "stor.db", null, 1) {
override fun onCreate(db: SQLiteDatabase?) {
var mytable =
"Create Table Userdata (Id integer primary key autoincrement, NAME text, EMAIL text, NUMBER text,PASSWORD text,BIRTH text)"
db!!.execSQL(mytable)
var usersave =
"Create Table Usersave (Id integer primary key autoincrement, NAME text, NUMBER text,Userid integer)"
db.execSQL(usersave)
}
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
}
fun InsertData(
name: String,
email: String,
number: String,
pass: String,
birth: String
): Boolean {
var sighup = "select * from Userdata where EMAIL = '$email' or NUMBER = '$number'"
var cursor1: Cursor = readableDatabase.rawQuery(sighup, null)
if (cursor1.count > 0) {
Toast.makeText(page, "USER ALrready Exit", Toast.LENGTH_SHORT).show()
return false
} else {
Toast.makeText(page, "SuccessFully Register", Toast.LENGTH_SHORT).show()
var instantdatatable =
"insert into Userdata (NAME , EMAIL , NUMBER ,PASSWORD ,BIRTH ) values('$name', '$email' ,'$number', '$pass' ,'$birth')"
writableDatabase.execSQL(instantdatatable)
return true
}
}
fun logincheak(loginemail: String, loginpass: String): Cursor {
var login =
"select * from Userdata where (EMAIL = '$loginemail' or NUMBER = '$loginemail') and PASSWORD = '$loginpass'"
var cursor: Cursor = readableDatabase.rawQuery(login, null)
return cursor
}
fun InsertDataContectSave(edname: String, ednumber: String, userid: Int) {
var readdata = "select * from Usersave where NAME = '$edname' or NUMBER = '$ednumber'"
var cursor2: Cursor = readableDatabase.rawQuery(readdata, null)
if (cursor2.count > 0) {
Toast.makeText(page, "Already Save Number", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(page, "Save Number", Toast.LENGTH_SHORT).show()
var ContectSave =
"insert into Usersave (NAME,NUMBER,Userid) values ('$edname','$ednumber','$userid')"
writableDatabase.execSQL(ContectSave)
}
}
fun ViewrMyconatct(userid: Int): Cursor {
var ss = "select * From Usersave where Userid = '$userid'"
var cursor = readableDatabase.rawQuery(ss, null);
return cursor
}
fun deletecontact(id: Int) {
var delet = " delete From Usersave where Id = '$id'"
writableDatabase.execSQL(delet)
}
fun UpdateDataContectSave(edname: String, ednumber: String, userid: Int) {
var update = "update Usersave set NAME = '$edname', NUMBER = '$ednumber' where Id = '$userid'"
writableDatabase.execSQL(update)
}
}
Upvotes: -1
Reputation: 95
Will they have their own DATABASE_NAME and DATABASE_TABLE one their phone?
-YES
Will the DATABASE_NAME and DATABASE_TABLE need to be different for every used that downloads the program and uses the Database on the app?
-NO
When your app is installed to a handset, it will create its own database accordingly to your configuration. You have nothing to worry about the configuration itself. It will work well as your emulator did.
Tip: You can actually use a mobile handset as your emulator. Your developing environment should detect it when you plugged it in with an adopter to your computer. E.g. Eclipse
Upvotes: 1