在SQLite中创建第二个表

时间:2013-02-21 01:43:56

标签: android database sqlite

我有一个数据库,其中一个表有两列名称,现在我想要创建一个包含两列年龄和性别的新表。我试图找到一些信息来创建另一个表但似乎很难。我怎样才能做到这一点?这是我的创建数据库方法:

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();
}

}

1 个答案:

答案 0 :(得分:1)

首先是一样的。再多一个execSQL。试试这个:

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);
    }
        }