Android Studio:如何在同一个数据库/ java类中拥有2个表?

时间:2016-09-26 15:06:20

标签: java android database sqlite android-studio

我有一个数据库,但我想为不同的类制作2个表。一个用于保存新用户数据,另一个用于保存用户会话。我试图开始工作的是由会议标题,时间,活动组成的表格。用户用户名,第一个,最后一个的表有效,但会话的表不起作用。最后,我想在一个数据库/ java类中放入2个表,即“ListBaseHelper”。我哪里出错了?

以下是包含数据库和表的Java类:

public class ListBaseHelper extends SQLiteOpenHelper {


private static final String DATABASE_NAME = "listBase.db";
private static final int DATABASE_VERSION = 2;
//Define a query for creating table so you don't get lost
private static final String CREATE_QUERY =
        "CREATE TABLE " + ListTable.TABLE_NAME + "(" + ListDbSchema.ListTable.USERNAME + " TEXT," +
                ListDbSchema.ListTable.FIRST + " TEXT," + ListDbSchema.ListTable.LAST + " TEXT);";

private static final String CREATE_QUERY2 =
        "CREATE TABLE " + ListTable.TABLE_NAME2 + "(" + ListDbSchema.ListTable.TITLE + " TEXT," +
                ListDbSchema.ListTable.TIME + " TEXT," + ListDbSchema.ListTable.ACTIVE + " TEXT);";

public ListBaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    //Log message
    Log.e("DATABASE OPERATIONS", "Database created /opened...");
}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(CREATE_QUERY);
    Log.e("DATABASE OPERATIONS", "Table created...");

    db.execSQL(CREATE_QUERY2);
    Log.e("DATABASE OPERATIONS", "Table created...");
}



public void addInformations(String username, String first, String last, SQLiteDatabase db) {
   ContentValues contentValues = new ContentValues();
    contentValues.put(ListTable.USERNAME, username);
    contentValues.put(ListTable.FIRST, first);
    contentValues.put(ListTable.LAST, last);
    db.insert(ListDbSchema.ListTable.TABLE_NAME, null, contentValues);
    Log.e("DATABASE OPERATIONS", "One row inserted...");
}



public void addSessionInfo(String title, String time, String active, SQLiteDatabase db) {
    ContentValues contentValues = new ContentValues();
    contentValues.put(ListTable.TITLE, title);
    contentValues.put(ListTable.TIME, time);
    contentValues.put(ListTable.ACTIVE, active);
    db.insert(ListDbSchema.ListTable.TABLE_NAME2, null, contentValues);
    Log.e("DATABASE OPERATIONS", "One row inserted...");
}


public Cursor getInformations(SQLiteDatabase db) {
    Cursor cursor;
    String[] projections = {ListTable.USERNAME, ListTable.FIRST, ListTable.LAST};

    cursor = db.query(ListDbSchema.ListTable.TABLE_NAME, projections, null, null, null, null, null);
    return cursor;
}

public Cursor SessionInformations(SQLiteDatabase db) {
    Cursor cursor2;
    String[] projections = {ListTable.TITLE, ListTable.TIME, ListTable.ACTIVE};

    cursor2 = db.query(ListDbSchema.ListTable.TABLE_NAME2, projections, null, null, null, null, null);
    return cursor2;
}


@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS contacts");

    onCreate(db);
}

}

当这个java类运行时,它会询问用户输入。

public class NewSessionActivity extends AppCompatActivity {

EditText SessionTitle, SessionTime, SessionActive;
Context context = this;
ListBaseHelper sessionbasehelper;
SQLiteDatabase sqliteDatabase;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_options_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.logoff_menu_item) {
        super.onOptionsItemSelected(item);
        Toast.makeText(this, "Logging You Off...", Toast.LENGTH_LONG).show();
        startActivity(new Intent(NewSessionActivity.this, LaunchActivity.class));
    }
    return true;
}

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

    SessionTitle = (EditText) findViewById(R.id.title);
    SessionTime = (EditText) findViewById(R.id.time);
    SessionActive = (EditText) findViewById(R.id.active);
}

public void btn_submit(View view) {
    {
        context = this;
        String title = SessionTitle.getText().toString();
        String time = SessionTime.getText().toString();
        String active = SessionActive.getText().toString();

        sessionbasehelper= new ListBaseHelper(context);
        sqliteDatabase = sessionbasehelper.getWritableDatabase();
        sessionbasehelper.addSessionInfo(title,time,active,sqliteDatabase);
        Toast.makeText(getBaseContext(), "New Session Saved!", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(NewSessionActivity.this, ViewAllSessionsActivity.class);
        startActivity(intent);
        sessionbasehelper.close();
    }
}

}

当java类运行时,它应该显示用户从上面的NewSessionActivity输入的数据。

public class ViewAllSessionsActivity extends AppCompatActivity{

//Create instance of database
ListBaseHelper myDb;
Button btn_logout;
ListView sessionlistView;
SQLiteDatabase sqLiteDatabase;
ListBaseHelper listbaseDbHelper;
Cursor cursor2;
SessionDataAdapter sessionDataAdapter;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_options_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.logoff_menu_item) {
        super.onOptionsItemSelected(item);
        Toast.makeText(this, "Logging You Off...", Toast.LENGTH_LONG).show();
        startActivity(new Intent(ViewAllSessionsActivity.this, LaunchActivity.class));
    }

    return true;
}

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


    sessionlistView = (ListView) findViewById(R.id.listViewSession);
    sessionDataAdapter = new SessionDataAdapter(getApplicationContext(),R.layout.all_session_layout);
    sessionlistView.setAdapter(sessionDataAdapter);
    listbaseDbHelper = new ListBaseHelper(getApplicationContext());
    sqLiteDatabase = listbaseDbHelper.getReadableDatabase();
    cursor2 = listbaseDbHelper.SessionInformations(sqLiteDatabase);
    if(cursor2.moveToFirst())
    {
        do {

            String title, time, active;
            title = cursor2.getString(0);
            time = cursor2.getString(1);
            active = cursor2.getString(2);
            SessionList sessionlist = new SessionList(title, time, active);
            sessionDataAdapter.add(sessionlist);


        }while (cursor2.moveToNext());
    }

    btn_logout = (Button)findViewById(R.id.complete_button);
    btn_logout.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            //Use the name of this class, and the name class where you want to be taken when the button is clicked.
            Intent intent = new Intent(ViewAllSessionsActivity.this, PaymentActivity.class);
            startActivity(intent);
        }
    });
}
}

这是我运行应用程序时的LogCat:

09-26 10:54:06.071 2712-2712/com.bignerdranch.android.assignment2 E/DATABASE OPERATIONS: Database created /opened...
09-26 10:54:24.365 2712-2712/com.bignerdranch.android.assignment2 E/DATABASE OPERATIONS: Database created /opened...
09-26 10:54:24.368 2712-2712/com.bignerdranch.android.assignment2 E/SQLiteLog: (1) no such table: table_name2
09-26 10:54:24.369 2712-2712/com.bignerdranch.android.assignment2 E/SQLiteDatabase: Error inserting title=Hi time=10:22 active=yes
                                                                                    android.database.sqlite.SQLiteException: no such table: table_name2 (code 1): , while compiling: INSERT INTO table_name2(title,time,active) VALUES (?,?,?)
                                                                                    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                                                                                    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                                                                                    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                                    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                                    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
                                                                                    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
                                                                                    at com.bignerdranch.android.assignment2.database.ListBaseHelper.addSessionInfo(ListBaseHelper.java:65)
                                                                                    at com.bignerdranch.android.assignment2.NewSessionActivity.btn_submit(NewSessionActivity.java:62)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                                    at android.view.View.performClick(View.java:4780)
                                                                                    at android.view.View$PerformClick.run(View.java:19866)
                                                                                    at android.os.Handler.handleCallback(Handler.java:739)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                    at android.os.Looper.loop(Looper.java:135)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
09-26 10:54:24.370 2712-2712/com.bignerdranch.android.assignment2 E/DATABASE OPERATIONS: One row inserted...
09-26 10:54:24.919 2712-2712/com.bignerdranch.android.assignment2 E/DATABASE OPERATIONS: Database created /opened...
09-26 10:54:24.941 2712-2712/com.bignerdranch.android.assignment2 E/SQLiteLog: (1) no such table: table_name2
09-26 10:54:24.951 2712-2712/com.bignerdranch.android.assignment2 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.bignerdranch.android.assignment2, PID: 2712
                                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.assignment2/com.bignerdranch.android.assignment2.ViewAllSessionsActivity}: android.database.sqlite.SQLiteException: no such table: table_name2 (code 1): , while compiling: SELECT title, time, active FROM table_name2
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                                    at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:135)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                                                 Caused by: android.database.sqlite.SQLiteException: no such table: table_name2 (code 1): , while compiling: SELECT title, time, active FROM table_name2
                                                                                    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                                                                                    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                                                                                    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                                    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
                                                                                    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
                                                                                    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
                                                                                    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
                                                                                    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
                                                                                    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
                                                                                    at com.bignerdranch.android.assignment2.database.ListBaseHelper.SessionInformations(ListBaseHelper.java:82)
                                                                                    at com.bignerdranch.android.assignment2.ViewAllSessionsActivity.onCreate(ViewAllSessionsActivity.java:60)
                                                                                    at android.app.Activity.performCreate(Activity.java:5990)
                                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                                    at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                    at android.os.Looper.loop(Looper.java:135) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
09-26 10:54:38.877 3535-3535/com.bignerdranch.android.assignment2 E/DATABASE OPERATIONS: Database created /opened...
09-26 10:54:38.892 3535-3535/com.bignerdranch.android.assignment2 E/SQLiteLog: (1) no such table: table_name2
09-26 10:54:38.893 3535-3535/com.bignerdranch.android.assignment2 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.bignerdranch.android.assignment2, PID: 3535
                                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.assignment2/com.bignerdranch.android.assignment2.ViewAllSessionsActivity}: android.database.sqlite.SQLiteException: no such table: table_name2 (code 1): , while compiling: SELECT title, time, active FROM table_name2
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                                    at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:135)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                                                 Caused by: android.database.sqlite.SQLiteException: no such table: table_name2 (code 1): , while compiling: SELECT title, time, active FROM table_name2
                                                                                    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                                                                                    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                                                                                    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                                    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
                                                                                    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
                                                                                    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
                                                                                    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
                                                                                    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
                                                                                    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
                                                                                    at com.bignerdranch.android.assignment2.database.ListBaseHelper.SessionInformations(ListBaseHelper.java:82)
                                                                                    at com.bignerdranch.android.assignment2.ViewAllSessionsActivity.onCreate(ViewAllSessionsActivity.java:60)
                                                                                    at android.app.Activity.performCreate(Activity.java:5990)
                                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                                    at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                    at android.os.Looper.loop(Looper.java:135) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

0 个答案:

没有答案