TranGia
TranGia

Reputation: 107

create a second table in SQLite

I have a database with one table which has two columns name and described now i want to create new table with two columns age and sex. I tried to find some info to create another table but it seem to hard. how can i do that? this is my 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 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));
    // return contact
    return contact;
}

public List<Name> getAllContacts() {
    List<Name> contactList = new ArrayList<Name>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Name contact = new Name();

            contact.SetName(cursor.getString(0));
            contact.SetDescribed(cursor.getString(1));
            // Adding contact to list
            contactList.add(contact);
        } while (cursor.moveToNext());
    }

    // return contact list
    return contactList;
}

// Getting contacts Count
public int getContactsCount() {
    String countQuery = "SELECT  * FROM " + TABLE;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    // return count
    return cursor.getCount();
}

}

Upvotes: 1

Views: 638

Answers (1)

QArea
QArea

Reputation: 4981

Its the same to first. Just one more execSQL. Try 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);
    }
        }

Upvotes: 1

Related Questions