在我的搜索中,我看到他的问题全都张贴了,但是没有一个答案为我解决了。我试图写到我的预订表,但是却得到:“表预订没有名为KEY_REST_STATE的列”。我尝试卸载应用程序,重新启动手机,并确保每次都增加数据库数量。我已经盯着这个代码几个小时了,找不到问题。请帮忙!
DatabaseHelper.java
package com.example.findaraunt;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import static android.content.ContentValues.TAG;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 10;
private static final String DATABASE_NAME = "theReserve";
private static final String TABLE_RES = "reservations";
private static final String KEY_RESID = "resid";
private static final String KEY_USERID = "userid";
private static final String KEY_REST_NAME = "rest_name";
private static final String KEY_REST_ADDRESS = "rest_address";
private static final String KEY_REST_CITY = "rest_city";
private static final String KEY_REST_STATE = "rest_state";
private static final String KEY_REST_ZIP = "rest_zip";
private static final String KEY_RES_DATE = "res_date";
private static final String KEY_RES_TIME = "res_time";
private static final String KEY_RES_SIZE = "res_size";
public DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
String CREATE_RES_TABLE = "CREATE TABLE " + TABLE_RES + "(" + KEY_RESID + " INTEGER PRIMARY KEY," + KEY_USERID + " INT,"+ KEY_REST_NAME +
" TEXT," + KEY_REST_ADDRESS + " TEXT," + KEY_REST_CITY + " TEXT," + KEY_REST_STATE + " TEXT," + KEY_REST_ZIP +
" INT," + KEY_RES_DATE + " TEXT," + KEY_RES_TIME + " TEXT," + KEY_RES_SIZE + " INT" + ")";
db.execSQL(CREATE_RES_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1){
Log.w(TAG, "Upgrading settings database from version " + i + " to "
+ i1);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_RES);
onCreate(db);
}
public void dropTable(){
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DROP TABLE IF EXISTS " + TABLE_RES);
}
void addReservation(Reservations reserv){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("KEY_USERID", reserv.getUserid());
values.put("KEY_REST_NAME", reserv.getRest_name());
values.put("KEY_REST_ADDRESS", reserv.getRest_address());
values.put("KEY_REST_CITY", reserv.getRest_city());
values.put("KEY_REST_STATE", reserv.getRest_state());
values.put("KEY_REST_ZIP", reserv.getRest_zip());
values.put("KEY_RES_DATE", reserv.getRes_date());
values.put("KEY_RES_TIME", reserv.getRes_time());
values.put("KEY_RES_SIZE", reserv.getRes_size());
db.insert(TABLE_RES, null, values);
db.close();
}
Reservations getReservation(int userid, int resid){
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.query(TABLE_RES, new String[]{KEY_RESID, KEY_USERID, KEY_REST_NAME,KEY_REST_ADDRESS,KEY_REST_CITY,KEY_REST_STATE,KEY_REST_ZIP,
KEY_RES_DATE,KEY_RES_TIME,KEY_RES_SIZE}, KEY_RESID + "=? AND " + KEY_USERID + "=?", new String[]{String.valueOf(resid)}, null, null, null, null);
if(cursor != null){
cursor.moveToFirst();
}
Reservations res = new Reservations(Integer.parseInt(cursor.getString(0)), Integer.parseInt(cursor.getString(1)),
cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5),
Integer.parseInt(cursor.getString(6)), cursor.getString(7), cursor.getString(8),
Integer.parseInt(cursor.getString(9)));
return res;
}
public List<Reservations> getAllReservations(){
List<Reservations> reservationsList = new ArrayList<>();
String selectQuery = "SELECT * FROM " + TABLE_RES;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()){
do{
Reservations reservations = new Reservations();
reservations.setResid(Integer.parseInt(cursor.getString(0)));
reservations.setUserid(Integer.parseInt(cursor.getString(1)));
reservations.setRest_name(cursor.getString(2));
reservations.setRest_address(cursor.getString(3));
reservations.setRest_city(cursor.getString(4));
reservations.setRest_state(cursor.getString(5));
reservations.setRest_zip(Integer.parseInt(cursor.getString(6)));
reservations.setRes_date(cursor.getString(7));
reservations.setRes_time(cursor.getString(8));
reservations.setRes_size(Integer.parseInt(cursor.getString(9)));
reservationsList.add(reservations);
}while (cursor.moveToNext());
}
return reservationsList;
}
}
ViewEditReservations.java
package com.example.findaraunt;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import java.util.List;
public class ViewEditReservations2 extends AppCompatActivity {
TextView textView;
String text = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_edit_reservations2);
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.nav_view);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(3);
menuItem.setChecked(true);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
Intent i1 = new Intent (ViewEditReservations2.this,ViewEditReservations.class);
startActivity(i1);
break;
case R.id.navigation_search:
break;
case R.id.navigation_profile:
Intent i3 = new Intent (ViewEditReservations2.this,ReviewRestaurant.class);
startActivity(i3);
break;
case R.id.navigation_logout:
break;
}
return false;
}
});
textView = (TextView)findViewById(R.id.textView);
DatabaseHelper db = new DatabaseHelper(this);
//inserting reservations
db.addReservation(new Reservations(1, "Applebees","123 Sesame St","Big Bird",
"AZ",12345,"7/1/2019","7:00pm",4));
db.addReservation(new Reservations(1,"Olive Garden","1 W Olive St","Tapenade","CA",
54321,"7/4/2019","12:00pm",6));
db.addReservation(new Reservations(2,"Marlows Tevern","88 Marlow Ave","Tavern","NY",
32143, "8/15/2019","10:00am",2));
//reading and displaying all reservations
List<Reservations> reservations = db.getAllReservations();
for (Reservations r : reservations){
String log = "userid: " + r.getUserid() +
", REST_NAME" + r.getRest_name() +
", REST_ADDRESS" + r.getRest_address() +
", REST_CITY" + r.getRest_city() +
", REST_STATE" + r.getRest_state() +
", REST_ZIP" + r.getRest_zip() +
", RES_DATE" + r.getRes_date() +
", RES_TIME" + r.getRes_time() +
", RES_SIZE" + r.getRes_size() + "\n";
text = text + log;
}
textView.setText(text);
}
}
Logcat
2019-07-02 03:37:34.710 12111-12111/? W/mple.findaraun: Unexpected CPU variant for X86 using defaults: x86
2019-07-02 03:37:35.524 12111-12111/com.example.findaraunt W/mple.findaraun: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
2019-07-02 03:37:35.525 12111-12111/com.example.findaraunt W/mple.findaraun: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
2019-07-02 03:37:35.986 12111-12161/com.example.findaraunt W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2019-07-02 03:37:36.048 12111-12111/com.example.findaraunt E/RecyclerView: No adapter attached; skipping layout
2019-07-02 03:37:40.730 12111-12111/com.example.findaraunt W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@7e4db54
2019-07-02 03:37:40.816 12111-12111/com.example.findaraunt W/ContentValues: Upgrading settings database from version 9 to 10
2019-07-02 03:37:40.826 12111-12111/com.example.findaraunt E/SQLiteLog: (1) table reservations has no column named KEY_REST_STATE
2019-07-02 03:37:40.832 12111-12111/com.example.findaraunt E/SQLiteDatabase: Error inserting KEY_REST_STATE=AZ KEY_RES_TIME=7:00pm KEY_REST_ADDRESS=123 Sesame St KEY_RES_DATE=7/1/2019 KEY_REST_NAME=Applebees KEY_USERID=1 KEY_REST_CITY=Big Bird KEY_REST_ZIP=12345 KEY_RES_SIZE=4
android.database.sqlite.SQLiteException: table reservations has no column named KEY_REST_STATE (code 1 SQLITE_ERROR): , while compiling: INSERT INTO reservations(KEY_REST_STATE,KEY_RES_TIME,KEY_REST_ADDRESS,KEY_RES_DATE,KEY_REST_NAME,KEY_USERID,KEY_REST_CITY,KEY_REST_ZIP,KEY_RES_SIZE) VALUES (?,?,?,?,?,?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514)
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:1562)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1433)
at com.example.findaraunt.DatabaseHelper.addReservation(DatabaseHelper.java:72)
at com.example.findaraunt.ViewEditReservations2.onCreate(ViewEditReservations2.java:57)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-07-02 03:37:40.843 12111-12111/com.example.findaraunt E/SQLiteLog: (1) table reservations has no column named KEY_REST_STATE
2019-07-02 03:37:40.846 12111-12111/com.example.findaraunt E/SQLiteDatabase: Error inserting KEY_REST_STATE=CA KEY_RES_TIME=12:00pm KEY_REST_ADDRESS=1 W Olive St KEY_RES_DATE=7/4/2019 KEY_REST_NAME=Olive Garden KEY_USERID=1 KEY_REST_CITY=Tapenade KEY_REST_ZIP=54321 KEY_RES_SIZE=6
android.database.sqlite.SQLiteException: table reservations has no column named KEY_REST_STATE (code 1 SQLITE_ERROR): , while compiling: INSERT INTO reservations(KEY_REST_STATE,KEY_RES_TIME,KEY_REST_ADDRESS,KEY_RES_DATE,KEY_REST_NAME,KEY_USERID,KEY_REST_CITY,KEY_REST_ZIP,KEY_RES_SIZE) VALUES (?,?,?,?,?,?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514)
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:1562)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1433)
at com.example.findaraunt.DatabaseHelper.addReservation(DatabaseHelper.java:72)
at com.example.findaraunt.ViewEditReservations2.onCreate(ViewEditReservations2.java:59)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
答案 0 :(得分:2)
您正在将值 KEY_REST_STATE 硬编码为列名称的字符串。您想要的是使用名为KEY_REST_STATE 的 String变量,该变量包含值为列名的 rest_state 。您需要在整个 addReservation 方法中应用此方法。
更改:-
values.put("KEY_REST_STATE", reserv.getRest_state());
至:-
values.put(KEY_REST_STATE, reserv.getRest_state());