How to add another table and insert values in existing SQLite Database in Android? -
i want add table in existing sqlite database android application. have created 1 table "dailytips" , want add table "healthytips". have tried different ways didn't succeed , done google search every code different mine. please 1 me in this.
here code:
package com.example.health; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.database.sqlite.sqlitedatabase.cursorfactory; public class sqliteadapter { public static final int database_version = 2; public static final string database_name = "healthdb"; public static final string table_daily = "dailytips"; public static final string column_id = "_id"; public static final string column_tips = "tips"; public static final string table_health="healthytips"; public static final string column_h_id = "_id"; public static final string column_h_tips = "tips"; //create table table_daily (id integer primary key, tips text not null); private static final string create_table_dailys = "create table " + table_daily + " (" + column_id + " integer primary key autoincrement, " + column_tips + " text not null);"; //create table table_healthy (id integer primary key, tips text not null); private static final string create_table_health= "create table " + table_health + " (" + column_h_id + " integer primary key autoincrement, " + column_h_tips + " text not null);"; private sqlitehelper sqlitehelper; private sqlitedatabase sqlitedatabase; private context context; public sqliteadapter(context c){ context = c; } public sqliteadapter opentoread() throws android.database.sqlexception { sqlitehelper = new sqlitehelper(context, database_name, null, database_version); sqlitedatabase = sqlitehelper.getreadabledatabase(); return this; } public sqliteadapter opentowrite() throws android.database.sqlexception { sqlitehelper = new sqlitehelper(context, database_name, null, database_version); sqlitedatabase = sqlitehelper.getwritabledatabase(); return this; } public void close(){ sqlitehelper.close(); } public long insert(string tips){ contentvalues contentvalues = new contentvalues(); contentvalues.put(column_tips, tips); return sqlitedatabase.insert(table_daily, null, contentvalues); } public int deleteall(){ return sqlitedatabase.delete(table_daily, null, null); } public cursor queueall(){ string[] columns = new string[]{column_id, column_tips}; cursor cursor = sqlitedatabase.query(table_daily, columns, null, null, null, null, null); return cursor; } public class sqlitehelper extends sqliteopenhelper { public sqlitehelper(context context, string name, cursorfactory factory, int version) { super(context, name, factory, version); } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub db.execsql(create_table_dailys); db.execsql(create_table_health); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub } }}
in oncreate() method,
db.execsql(create_database_table_1); db.execsql(create_database_table_2);
update in program.
@override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub // drop older table if existed db.execsql("drop table if exists database_table_1"); db.execsql("drop table if exists database_table_2"); // create tables again oncreate(db); }
note: need clear database before run application.
Comments
Post a Comment