如何分别显示两个表条目(我的一个表中显示所有条目)

时间:2013-03-29 00:28:51

标签: android android-sqlite

我的数据库中有两个表。表2的条目与其自己的条目

一起显示在一个表1中

例如,如果表1 DATABASE_TABLE和表2 DATBASE_TABLE2有5个条目EACH,当我输入并查看数据库时,表1带有10个条目,表格显示为空。

我的DatabaseManager.java,它处理数据库的所有操作

package com.example.draft;

import java.sql.SQLException;

import android.content.ContentValues;
import android.content.Context;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

// Class handling all database operations

public class DatabaseManager 
{
//setting variables to use later on in tables and database


public static final String KEY_EXERCISENAME = "exercisename";
public static final String KEY_DURATION = "duration";

private static final String DATABASE_NAME = "MyDatabase";
private static final String DATABASE_TABLE = "weekOne";
private static final String DATABASE_TABLE2 = "weektwo";

private static final int DATABASE_VERSION = 3;


//setting variables to reference DbHelper, its Context, and SQLiteDatabase

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

//sub class which creates the database

private static class DbHelper extends SQLiteOpenHelper
{

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

        // TODO Auto-generated constructor stub
    }

    //method to create the database and table

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        // TODO Auto-generated method stub
        db.execSQL
        ("CREATE TABLE " + DATABASE_TABLE + " (" +
            KEY_EXERCISENAME + " TEXT NOT NULL, " +
            KEY_DURATION + " TEXT NOT NULL);"
        );

        db.execSQL
        ("CREATE TABLE " + DATABASE_TABLE2 + " (" +
            KEY_EXERCISENAME + " TEXT NOT NULL, " +
            KEY_DURATION + " TEXT NOT NULL);"
        );

    }

    //method to show the table if it exists

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        // TODO Auto-generated method stub

        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE2);

        onCreate(db);
    }

}

public DatabaseManager(Context c)
{
    ourContext = c; 
}

public DatabaseManager open() 
{
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;    
}

//creating entry in table for treadmill in table week 1 with the help of ContentValues

public long createEntry(String treadmillTimings) 
{
    // TODO Auto-generated method stub

    ContentValues cv = new ContentValues();

    //enterting each exercise name corresponding to their respective edit Texts

    cv.put(KEY_EXERCISENAME, "Treadmill");
    cv.put(KEY_DURATION, treadmillTimings);

    return ourDatabase.insert(DATABASE_TABLE, null,cv);

}

//creating entry in table for stepperTimings in table week 1 with the help of ContentValues

public long week1createEntry1 (String stepperTimings)
{
    ContentValues cv1 = new ContentValues();
    cv1.put(KEY_EXERCISENAME, "Stepper");
    cv1.put(KEY_DURATION, stepperTimings);
    return ourDatabase.insert(DATABASE_TABLE, null,cv1);

}

//creating entry in table for Stationary Rowing in table week 1 with the help of ContentValues

public long week1createEntry2 (String stationaryRowingTimings)
{
    ContentValues cv2 = new ContentValues();
    cv2.put(KEY_EXERCISENAME, "Stationary Rowing");
    cv2.put(KEY_DURATION, stationaryRowingTimings);

    return ourDatabase.insert(DATABASE_TABLE, null,cv2);

}

//creating entry in table for exercise bike in table week 1 with the help of ContentValues

public long week1createEntry3 (String exerciseBikeTimings)
{
    ContentValues cv3 = new ContentValues();
    cv3.put(KEY_EXERCISENAME, "Exercise Bike");
    cv3.put(KEY_DURATION, exerciseBikeTimings);
    return ourDatabase.insert(DATABASE_TABLE, null,cv3);

}

//creating entry in table for elliptical trainer in table week 1 with the help of ContentValues

public long week1createEntry4 (String ellipticalTrainerTimings)
{
    ContentValues cv4 = new ContentValues();
    cv4.put(KEY_EXERCISENAME, "Stationary Rowing");
    cv4.put(KEY_DURATION, ellipticalTrainerTimings);
    return ourDatabase.insert(DATABASE_TABLE, null,cv4);

}

//displaying/reading data in the table using cursor

public String week1getData() 
{
    // TODO Auto-generated method stub
    String[] columns = new String[]{KEY_EXERCISENAME, KEY_DURATION};
    Cursor cur = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);

    //creating a result(string type variable) to store the text and display it.

    String result = "";

    int iExerciseName = cur.getColumnIndex(KEY_EXERCISENAME);
    int iDuration = cur.getColumnIndex(KEY_DURATION);

    // cursor start from the first position, keeps moving to the next as long as the position in not after that last.

    for(cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext())
    {
        /*getting the rows, exercise name and duration in the tables of database and setting it to result. 
          .The next time it loops, it will still have the previous result*/

        result = result + cur.getString(iExerciseName) + "                            " + cur.getString(iDuration) + "\n";
    }

    return result;

}

public long week2createEntry(String treadmillTimingsweek2) 
{
    // TODO Auto-generated method stub

    ContentValues cv = new ContentValues();

    //Entering each exercise name corresponding to their respective edit Texts

    cv.put(KEY_EXERCISENAME, "Treadmill");
    cv.put(KEY_DURATION, treadmillTimingsweek2);

    return ourDatabase.insert(DATABASE_TABLE2, null,cv);

}

//creating entry in table for stepperTimings in table week 1 with the help of ContentValues

public long week2createEntry1 (String stepperTimingsweek2)
{
    ContentValues cv1 = new ContentValues();
    cv1.put(KEY_EXERCISENAME, "Stepper");
    cv1.put(KEY_DURATION, stepperTimingsweek2);
    return ourDatabase.insert(DATABASE_TABLE2, null,cv1);

}

//creating entry in table for Stationary Rowing in table week 1 with the help of ContentValues

public long week2createEntry2 (String stationaryRowingTimingsweek2)
{
    ContentValues cv2 = new ContentValues();
    cv2.put(KEY_EXERCISENAME, "Stationary Rowing");
    cv2.put(KEY_DURATION, stationaryRowingTimingsweek2);

    return ourDatabase.insert(DATABASE_TABLE2, null,cv2);

}

//creating entry in table for exercise bike in table week 1 with the help of ContentValues

public long week2createEntry3 (String exerciseBikeTimingsweek2)
{
    ContentValues cv3 = new ContentValues();
    cv3.put(KEY_EXERCISENAME, "Exercise Bike");
    cv3.put(KEY_DURATION, exerciseBikeTimingsweek2);
    return ourDatabase.insert(DATABASE_TABLE2, null,cv3);

}

//creating entry in table for elliptical trainer in table week 1 with the help of ContentValues

public long week2createEntry4 (String ellipticalTrainerTimingsweek2)
{
    ContentValues cv4 = new ContentValues();
    cv4.put(KEY_EXERCISENAME, "Stationary Rowing");
    cv4.put(KEY_DURATION, ellipticalTrainerTimingsweek2);
    return ourDatabase.insert(DATABASE_TABLE2, null,cv4);

}

public String week2getData() 
{
    // TODO Auto-generated method stub
    String[] columns = new String[]{KEY_EXERCISENAME, KEY_DURATION};
    Cursor cur = ourDatabase.query(DATABASE_TABLE2, columns, null, null, null, null, null);

    //creating a result(string type variable) to store the text and display it.

    String result = "";

    int iExerciseName = cur.getColumnIndex(KEY_EXERCISENAME);
    int iDuration = cur.getColumnIndex(KEY_DURATION);

    // cursor start from the first position, keeps moving to the next as long as the position in not after that last.

    for(cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext())
    {
        /*getting the rows, exercise name and duration in the tables of database and setting it to result. 
          .The next time it loops, it will still have the previous result*/

        result = result + cur.getString(iExerciseName) + "                            " + cur.getString(iDuration) + "\n";
    }

    return result;

}

我正在显示DATABASE_TABLE条目的textView是android:id = @+id/dbinfo中的DATABASE_TABLE2android:id = "@+id/week2dbinfo条目

我想要的是DATABASE_TABLEandroid:id = @+id/dbinfo中显示5个条目,DATABASE TABLE2android:id = "@+id/week2dbinfo中显示5个条目

请帮忙

1 个答案:

答案 0 :(得分:0)

我使用第1周week1getData()的方法调用第2周显示条目的方法,因此将其更改为week2getData