在SQLite中显示具有连接ID的多个表中的数据

时间:2015-10-31 07:42:11

标签: java android sqlite cursor

我的SQLite数据库中有两个表,第一个用户和第二个作业,用户可以在其中执行多个作业

users表格中的数据

id      name
1        Me
2       You

通过以下方式生成id id整数主键自动增量

jobs表格中的数据

id     jobname     userid
1       JobA          2
2       JobB          2
3       JobC          1

通过以下方式生成id id整数主键自动增量

从{strong>用户表

获取user_id的地方

现在,我想逐个loop通过所有用户和工作表,如果用户有一些工作要做,那么需要显示 Toast

JobC assigned to user1

DatabaseHandler.java:

public class DatabaseHandler extends SQLiteOpenHelper {

..................

public DatabaseHandler(Context context) {

   super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

    db.execSQL("CREATE TABLE " + TABLE_USERS + 
              "(id integer primary key autoincrement," +
              " name text);");

        db.execSQL("CREATE TABLE " + TABLE_JOBS + 
              "(id integer primary key autoincrement," +
              " jobname text" +
              " userid long);");        
}

// Insert data into users table
public long InsertUsers(String name) {

     try {
        SQLiteDatabase db;
        db = this.getWritableDatabase(); // Write Data

        ContentValues Val = new ContentValues();
        Val.put("name", name); 

        long rows = db.insert(TABLE_USERS, null, Val);

        db.close();
        return rows; // return rows inserted.

     } catch (Exception e) {
        return -1;
     }
}

    // Insert data into jobs table
public long InsertJobs(String jobname, long userid) {

     try {
        SQLiteDatabase db;
        db = this.getWritableDatabase(); // Write Data

        ContentValues Val = new ContentValues();
        Val.put("jobname", jobname); 
        Val.put("userid", userid); 

        long rows = db.insert(TABLE_JOBS, null, Val);

        db.close();
        return rows; // return rows inserted.

     } catch (Exception e) {
        return -1;
     }
}   

public String[] getUsersId() {

    String selectQuery = "SELECT id FROM " + TABLE_USERS;
          SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.rawQuery(selectQuery, null);
             String[] data = null;

    if (cursor.moveToFirst()) {
        do {

            int id_row=cursor.getInt(cursor.getColumnIndex("id"));
                Log.d("TAG","id is ::"+id_row);             

        } while (cursor.moveToNext());
    }

    db.close();
    return data;
}

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

}

在MainActivity.java中:

    DatabaseHandler dh;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

            dh = new DatabaseHandler(MainActivity.this);

            buttonGetData.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                dh.getUsersId();
                }
            });

   }

2 个答案:

答案 0 :(得分:2)

您可以使用join:

来完成
db.rawQuery("SELECT j.jobname, u.name FROM jobs j JOIN users u ON j.userid=u.id");

答案 1 :(得分:0)

我不会给你确切的代码但是尝试执行与此类似的查询。在提出问题之前做一点研究。

String newQuery="SELECT db1.your_column db2.you_coloum2 from table1 db1, table2 db2 where db2.u_id=db1.id;

使用Cursor

获取结果