2013-03-29 27 views
0

我在我的數據庫中有兩個表。例如,如果表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/dbinfoandroid:id = "@+id/week2dbinfo

DATABASE_TABLE2條目我要的是DATABASE_TABLE顯示5項在android:id = @+id/dbinfoDATABASE TABLE2顯示5個條目android:id = "@+id/week2dbinfo

請h ELP

+0

你看實際的表?也許你沒有插入到正確的表格中開始。也就是說,你的數據庫模式不是很好。爲什麼不把所有東西放在* one *表中,添加一個'weekId',然後你可以基於'weekId = 1'或'weekId = 2'查詢 - 並且你不必每週都添加一個表未來。 – 323go

+0

我按照你的方式取得了我的成績,但仍然沒有按預期的方式出現 – user2179825

+0

考慮到你的任何一個表上沒有主鍵,是否有可能將值插入兩次?如果沒有,請在您實際顯示數據的位置添加代碼。 – 323go

回答

0

我用week1 week1getData()的方法調用第二週顯示條目的方法,所以把它改爲week2getData

相關問題