多表数据库SQLite android

时间:2014-02-14 12:43:53

标签: android database sqlite multi-table

我是数据库(SQLite)的新手,我正在尝试在android中创建一个多表数据库。每个表都有相同的行(id,name,ph_number)。每个表都存储一个“联系”对象。

当我想使用方法addContactDatos(Contact contact,int partido)将联系人存储在一个表中时,它存储在每个表中,而不仅仅存储在我想要的表中。我该如何解决?

这是处理程序:

public class DatabaseHandler extends SQLiteOpenHelper {

// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "contactsManager";

// Contacts Tables  names
private static final String TABLE_PRIMERO = "contacts";
private static final String TABLE_SEGUNDO = "contacts";
private static final String TABLE_TERCERO = "contacts";  
private static final String TABLE_CUARTO = "contacts";

// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PH_NO = "phone_number";

int oldVersion=1;

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {

    String CREATE_CONTACTS_TABLE1 = "CREATE TABLE_PRIMERO " + TABLE_PRIMERO + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_PH_NO + " TEXT" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE1);


    String CREATE_CONTACTS_TABLE2 = "CREATE TABLE_SEGUNDO " + TABLE_SEGUNDO + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_PH_NO + " TEXT" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE2);


    String CREATE_CONTACTS_TABLE3 = "CREATE TABLE_TERCERO " + TABLE_TERCERO + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_PH_NO + " TEXT" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE3);


    String CREATE_CONTACTS_TABLE4 = "CREATE TABLE_CUARTO " + TABLE_CUARTO + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_PH_NO + " TEXT" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE4);    
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRIMERO);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_SEGUNDO);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_TERCERO);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CUARTO);

    // Create tables again
    onCreate(db);
}

public void addContactDatos(Contact contact, int partido) {

    SQLiteDatabase db = this.getWritableDatabase();

    // Inserting Row
    if (partido == 1) {
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, contact.getName()); // Contact Name
        values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone Number

        db.insert(TABLE_PRIMERO, null, values);
        db.close(); // Closing database connection
        Log.d("Entra","MAL");
    }

 // Inserting Row
    if (partido == 2) {
         ContentValues values = new ContentValues();
         values.put(KEY_NAME, contact.getName()); // Contact Name
         values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone Number
        db.insert(TABLE_SEGUNDO, null, values);
        db.close(); // Closing database connection
        Log.d("Entra","2");
              }

 // Inserting Row
    if (partido == 3) {
         ContentValues values = new ContentValues();
         values.put(KEY_NAME, contact.getName()); // Contact Name
         values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone Number
        db.insert(TABLE_TERCERO, null, values);
        db.close(); // Closing database connection
        Log.d("Entra","MAL");
    }

 // Inserting Row
    if (partido == 4) {
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, contact.getName()); // Contact Name
        values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone Number
        db.insert(TABLE_CUARTO, null, values);
        db.close(); // Closing database connection
        Log.d("Entra","MAL");
    }



}


// Getting All Contacts
public List<Contact> getAllContacts(int partido) {

   List<Contact> contactList = new ArrayList<Contact>();
   String selectQuery="";
// Select All Query
   SQLiteDatabase db = this.getWritableDatabase();
   Cursor cursor = db.rawQuery(selectQuery, null); 

   switch (partido){
    case 1:
        selectQuery = "SELECT  * FROM " + TABLE_PRIMERO;  

         // looping through all rows and adding to list
         if (cursor.moveToFirst()) {
             do {
                 Contact contact = new Contact();
                 contact.setID(Integer.parseInt(cursor.getString(0)));
                 contact.setName(cursor.getString(1));
                 contact.setPhoneNumber(cursor.getString(2));
                 // Adding contact to list
                 contactList.add(contact);
             } while (cursor.moveToNext());
         }
    break;

    case 2:
        selectQuery = "SELECT  * FROM " + TABLE_SEGUNDO;   

         // looping through all rows and adding to list
         if (cursor.moveToFirst()) {
             do {
                 Contact contact = new Contact();
                 contact.setID(Integer.parseInt(cursor.getString(0)));
                 contact.setName(cursor.getString(1));
                 contact.setPhoneNumber(cursor.getString(2));
                 // Adding contact to list
                 contactList.add(contact);
             } while (cursor.moveToNext());
         }
        break;

    case 3:
        selectQuery = "SELECT  * FROM " + TABLE_TERCERO;   

         // looping through all rows and adding to list
         if (cursor.moveToFirst()) {
             do {
                 Contact contact = new Contact();
                 contact.setID(Integer.parseInt(cursor.getString(0)));
                 contact.setName(cursor.getString(1));
                 contact.setPhoneNumber(cursor.getString(2));
                 // Adding contact to list
                 contactList.add(contact);
             } while (cursor.moveToNext());
         }
        break;

    case 4:
        selectQuery = "SELECT  * FROM " + TABLE_CUARTO;   


         // looping through all rows and adding to list
         if (cursor.moveToFirst()) {
             do {
                 Contact contact = new Contact();
                 contact.setID(Integer.parseInt(cursor.getString(0)));
                 contact.setName(cursor.getString(1));
                 contact.setPhoneNumber(cursor.getString(2));
                 // Adding contact to list
                 contactList.add(contact);
             } while (cursor.moveToNext());
         }
        break;

  }


   // return contact list
   return contactList;
}

public void deleteDB() {

    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_PRIMERO, null, null);
    db.delete(TABLE_SEGUNDO, null, null);
    db.delete(TABLE_TERCERO, null, null);
    db.delete(TABLE_CUARTO, null, null);

}

这是对象模型联系人:     公共课程联系{

 //private variables
int _id;
String _name;
String _phone_number;

// Empty constructor
public Contact(){

}
// constructor
public Contact(int id, String name, String _phone_number){
    this._id = id;
    this._name = name;
    this._phone_number = _phone_number;
}

// constructor
public Contact(String name, String _phone_number){
    this._name = name;
    this._phone_number = _phone_number;
}
// getting ID
public int getID(){
    return this._id;
}

// setting id
public void setID(int id){
    this._id = id;
}

// getting name
public String getName(){
    return this._name;
}

// setting name
public void setName(String name){
    this._name = name;
}

// getting phone number
public String getPhoneNumber(){
    return this._phone_number;
}

// setting phone number
public void setPhoneNumber(String phone_number){
    this._phone_number = phone_number;
}
}

以任何方式做我想要的扫管笏。我真的很感激一点帮助。 非常感谢你。

2 个答案:

答案 0 :(得分:5)

必须使用不同的表名!!

即:

// Contacts Tables  names
private static final String TABLE_PRIMERO = "contacts_1";
private static final String TABLE_SEGUNDO = "contacts_2";
private static final String TABLE_TERCERO = "contacts_3";  
private static final String TABLE_CUARTO = "contacts_4";

否则,您始终在同一个表(您创建的唯一一个表)上工作,名为“contacts”

答案 1 :(得分:0)

您的所有表名都是相同的

private static final String TABLE_PRIMERO = "contacts";
private static final String TABLE_SEGUNDO = "contacts";
private static final String TABLE_TERCERO = "contacts";  
private static final String TABLE_CUARTO = "contacts";

这里你没有多表你只有一个名为contacts的表

更改表名

private static final String TABLE_PRIMERO = "contacts_primero";
private static final String TABLE_SEGUNDO = "contacts_segundo";
private static final String TABLE_TERCERO = "contacts_tercero";  
private static final String TABLE_CUARTO = "contacts_cuarto";