SQLite数据库:有没有办法将相同的数据放入2个表中?

时间:2018-03-03 19:51:00

标签: android sqlite android-sqlite

我试图让我的数据同时插入2个表中。第一个表用于用户,第二个表用于管理员。我还为管理员插入了额外的2列供以后使用。我只想知道如何在同一时间内在2个表中插入数据。每当我运行我的应用程序时,第二个表格都没有我插入的数据。

package com.radixappointment.radix;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseInfo extends SQLiteOpenHelper {

/*Information Database*/
public static final String DATABASE_NAME = "information.db";
public static final String TABLE_NAME = "information_table";
public static final String ADMIN_NAME = "admin_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "COMPANY_NAME";
public static final String COL_3 = "CLIENT_NAME";
public static final String COL_4 = "CONTACT";
public static final String COL_5 = "TIME";
public static final String COL_6 = "STATUS";

public DatabaseInfo(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, COMPANY_NAME TEXT, CLIENT_NAME TEXT, CONTACT TEXT)");
  db.execSQL("CREATE TABLE " + ADMIN_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, COMPANY_NAME TEXT, CLIENT_NAME TEXT, CONTACT TEXT, TIME TEXT, STATUS TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    db.execSQL("DROP TABLE IF EXISTS " + ADMIN_NAME);
    onCreate(db);
}

public boolean insertData(String Company, String Client, String Contact) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_2, Company);
    contentValues.put(COL_3, Client);
    contentValues.put(COL_4, Contact);
    long result = db.insert(TABLE_NAME, null, contentValues);
    if (result == -1)
        return false;
    else
        return true;

}
public boolean insertDataAdmin(String Company, String Client, String Contact, String Time, String Status) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_2, Company);
    contentValues.put(COL_3, Client);
    contentValues.put(COL_4, Contact);
    contentValues.put(COL_5, Time);
    contentValues.put(COL_6, Status);
    long adminresult = db.insert(TABLE_NAME, null, contentValues);
    if (adminresult == -1)
        return false;
    else
        return true;

}

public Cursor getAllData() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
    return res;
}

public Cursor getAllDataAdmin() {
    SQLiteDatabase dbadmin = this.getWritableDatabase();
    Cursor admin = dbadmin.rawQuery("select * from " + ADMIN_NAME, null);
    return admin;
}
}

1 个答案:

答案 0 :(得分:0)

我认为您指定了错误的表格,因此尝试将所有行/数据插入用户的表格而不是< strong> admin 表。此额外尝试将失败,但会被困,因为用户表中不存在基于时间的列。

要解决此问题,请在insertDataAdmin方法中更改: -

long adminresult = db.insert(TABLE_NAME, null, contentValues);

: -

long adminresult = db.insert(ADMIN_NAME, null, contentValues);