Friday, 15 March 2013

android - create a second table in SQLite -



android - create a second table in SQLite -

i have database 1 table has 2 columns name , described want create new table 2 columns age , sex. tried find info create table seem hard. how can that? create database method:

public class dataprovider extends sqliteopenhelper { // database version private static final int database_version = 1; // database name private static final string database_name = "contactsmanager"; // contacts table name private static final string table = "contacts"; public dataprovider(context context) { super(context, database_name, null, database_version); } // contacts table columns names private static final string key_name = "name"; private static final string key_decribed = "described"; @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub string create_contacts_table = "create table " + table + "(" +key_name + " text," + key_decribed + " text" + ")"; db.execsql(create_contacts_table); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub db.execsql("drop table if exists " + table); // create tables 1 time again oncreate(db); } public void addcontact(name contact) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_name, contact.getname()); // contact name values.put(key_decribed, contact.getdescribed()); // contact described // inserting row db.insert(table, null, values); db.close(); // closing database connection } // getting single contact public name getcontact(int id) { sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.query(table, new string[] { key_name, key_decribed }, null, new string[] { string.valueof(id) }, null, null, null, null); if (cursor != null) cursor.movetofirst(); name contact = new name(cursor.getstring(0), cursor.getstring(1)); // homecoming contact homecoming contact; } public list<name> getallcontacts() { list<name> contactlist = new arraylist<name>(); // select query string selectquery = "select * " + table; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(selectquery, null); // looping through rows , adding list if (cursor.movetofirst()) { { name contact = new name(); contact.setname(cursor.getstring(0)); contact.setdescribed(cursor.getstring(1)); // adding contact list contactlist.add(contact); } while (cursor.movetonext()); } // homecoming contact list homecoming contactlist; } // getting contacts count public int getcontactscount() { string countquery = "select * " + table; sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery(countquery, null); cursor.close(); // homecoming count homecoming cursor.getcount(); }

}

its same first. 1 more execsql. seek this:

import android.content.context; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.provider.basecolumns;

public class dbhelper extends sqliteopenhelper {

public static interface human extends basecolumns { string table_name = "human"; string name = "name"; string decribed = "described"; } public static interface describtion extends basecolumns { string table_name = "describtion"; string age = "age"; string sex = "sex"; } private static final string create_table = "create table if not exists "; private static final string database_name = "contactsmanager"; private static final int database_version = 1; private static final string unique = "unique on conflict abort"; private static final string create_table_human = create_table + human.table_name + " (" + human._id + " integer primary key," + human.name + " text, " + human.decribed + " text);"; private static final string create_table_describtion = create_table + describtion.table_name + " (" + describtion._id + " integer primary key," + describtion.age + " integer, " + describtion.sex + " text);"; public dbhelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { db.execsql(create_table_human); db.execsql(create_table_describtion); } @override public void onupgrade(sqlitedatabase db, int arg1, int arg2) { db.execsql("drop table if exists " + human.table_name); db.execsql("drop table if exists " + describtion.table_name); oncreate(db); } }

android database sqlite

No comments:

Post a Comment