java - Multiple tables with same structure - Sqlite -


i have table called messages.but want create separate table separate users. ex: if username ciddarth want table named ciddarth same structure messages

public class dbhandler extends sqliteopenhelper {  sqlitedatabase db; // static variables // database version private static final int database_version = 1;  // database name private static final string database_name = "socialnetwork";  // contacts table name private string table_messages = "messages";  // contacts table columns names private static final string key_id = "id"; private static final string key_message = "message"; private static final string key_send = "send"; private static final string key_status = "status"; private static final string key_time = "time";  public dbhandler(context context) {     super(context, database_name, null, database_version);     //table_messages=name; }  // creating tables @override public void oncreate(sqlitedatabase db) {     this.db=db;     string create_message_table = "create table " + table_messages + "("             + key_id + " integer primary key," + key_message + " mediumtext,"             + key_send + " text," + key_status + " integer,"+key_time+" datetime default (datetime(current_timestamp, 'localtime'))"+")";     db.execsql(create_message_table); }  // upgrading database @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {     // drop older table if existed     db.execsql("drop table if exists " + table_messages);      // create tables again     oncreate(db); }  /**  * crud(create, read, update, delete) operations  */  // adding new contact void addmessage(message message) {     sqlitedatabase db = this.getwritabledatabase();      contentvalues values = new contentvalues();     values.put(key_message, message.getmessage());     values.put(key_send, message.getsend());     values.put(key_status, message.getstatus());      // inserting row     db.insert(table_messages, null, values);     db.close(); // closing database connection }  // getting single contact message getmessage(int id) {     sqlitedatabase db = this.getreadabledatabase();      cursor cursor = db.query(table_messages, new string[] { key_id,                     key_message, key_send }, key_id + "=?",             new string[] { string.valueof(id) }, null, null, null, null);     if (cursor != null)         cursor.movetofirst();      message message = new message(integer.parseint(cursor.getstring(0)),             cursor.getstring(1), cursor.getstring(2),cursor.getstring(4),integer.parseint(cursor.getstring(3)));     return message;  }  // getting contacts public list<message> getallcontacts() {     list<message> msglist = new arraylist<message>();     // select query     string selectquery = "select  * " + table_messages;      sqlitedatabase db = this.getwritabledatabase();     cursor cursor = db.rawquery(selectquery, null);      // looping through rows , adding list     if (cursor.movetofirst()) {         {             message message = new message();             message.setid(integer.parseint(cursor.getstring(0)));             message.setmessage(cursor.getstring(1));             message.setsend(cursor.getstring(2));             message.setstatus(cursor.getint(3));             message.settime(cursor.getstring(4));             // adding contact list             msglist.add(message);         } while (cursor.movetonext());     }      // return contact list     return msglist; }  // updating single contact public int updatemessage(message message) {     sqlitedatabase db = this.getwritabledatabase();      contentvalues values = new contentvalues();     values.put(key_message, message.getmessage());     values.put(key_send, message.getsend());      // updating row     return db.update(table_messages, values, key_id + " = ?",             new string[] { string.valueof(message.getid()) }); }  // deleting single contact public void deletemessage(int id) {     sqlitedatabase db = this.getwritabledatabase();     db.delete(table_messages, key_id + " = ?",             new string[] { string.valueof(id) });     db.close(); }   // getting contacts count public int getmessagecount() {     string countquery = "select  * " + table_messages;     sqlitedatabase db = this.getreadabledatabase();     cursor cursor = db.rawquery(countquery, null);     cursor.close();      // return count     return cursor.getcount(); }  public void deleteall() {     sqlitedatabase db = this.getwritabledatabase();     db.execsql("delete "+ table_messages);     db.close(); }  public void deletetable() {     sqlitedatabase db = this.getreadabledatabase();     db.execsql("drop table if exists " + table_messages);     db.close(); }  public void changestatus(int status,int id) {     sqlitedatabase db = this.getwritabledatabase();     contentvalues cv = new contentvalues();     cv.put(key_status,status);     db.update(table_messages, cv, "id="+id, null);     db.close(); } } 

now want change tables_messages related user how this?? if change value of table_messages ciddarth still doesn't work.

make seperate method...

`createnewtable(string tablename){      sqliteopendatabase db = this.getwriteabledatabase();      string create_message_table = "create table " + tablename + "("          + key_id + " integer primary key," + key_message + " mediumtext,"           + key_send + " text," + key_status + " integer,"+key_time+          "datetime default (datetime(current_timestamp, 'localtime'))"+")";  db.execsql(create_message_table);` 

and add string tablename other crud methods.

edit: forgot this.getwritabledatabase();


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -