2017-01-08 16 views
0

私は間違って作業をしましたか?SQLiteException:そのようなテーブルがありません。Android Studio

私はデータベースにデータを挿入しようとすると、私はインサータToastメッセージが表示されますが、ログファイルに私はエラーが表示されます。

no such table: reservation (code 1): , while compiling: INSERT INTO reservation(phone,address,surname,name,start,destination) VALUES (?,?,?,?,?,?) at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)

DBHelper.java

package com.example.demir.carsharing; 

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 android.widget.TextView; 


public class DbHelper extends SQLiteOpenHelper { 
    public static final String TAG = DbHelper.class.getSimpleName(); 
    public static final String DB_NAME = "carsharing.db"; 
    public static final int DB_VERSION = 1; 

    public static final String USER_TABLE = "users"; 
    public static final String COLUMN_ID = "_id"; 
    public static final String COLUMN_EMAIL = "email"; 
    public static final String COLUMN_PASS = "password"; 

    public static final String RES_TABLE="reservation"; 
    public static final String COLUMN_NAME="name"; 
    public static final String COLUMN_SURNAME="surname"; 
    public static final String COLUMN_ADDRESS="address"; 
    public static final String COLUMN_PHONE="phone"; 
    public static final String COLUMN_START="start"; 
    public static final String COLUMN_DESTINATION="destination"; 


    /* 
create table users(
    id integer primary key autoincrement, 
    email text, 
    password text); 
    */ 
    public static final String CREATE_TABLE_USERS = "CREATE TABLE " + USER_TABLE + "(" 
      + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
      + COLUMN_EMAIL + " TEXT," 
      + COLUMN_PASS + " TEXT);"; 

    public static final String CREATE_TABLE_RESERVATION = "CREATE TABLE " + RES_TABLE + "(" 
      + COLUMN_NAME + " TEXT," 
      + COLUMN_SURNAME + " TEXT," 
      + COLUMN_ADDRESS + " TEXT," 
      + COLUMN_PHONE + " TEXT," 
      + COLUMN_START + " TEXT," 
      + COLUMN_DESTINATION + " TEXT);"; 


    public DbHelper(Context context) { 
     super(context, DB_NAME, null, DB_VERSION); 

    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(CREATE_TABLE_USERS); 
     db.execSQL(CREATE_TABLE_RESERVATION); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXIST" + USER_TABLE); 
     onCreate(db); 

     db.execSQL("DROP TABLE IF EXIST" + RES_TABLE); 
     onCreate(db); 
    } 


    public void addUser(String email, String password) { 
     SQLiteDatabase db = this.getWritableDatabase(); 

     ContentValues values = new ContentValues(); 
     values.put(COLUMN_EMAIL, email); 
     values.put(COLUMN_PASS, password); 

     long id = db.insert(USER_TABLE, null, values); 
     db.close(); 

     Log.d(TAG, "user inserted" + id); 
    } 

    public boolean getUser(String email, String pass) { 
     //HashMap<String, String> user = new HashMap<String, String>(); 
     String selectQuery = "select * from " + USER_TABLE + " where " + 
       COLUMN_EMAIL + " = " + "'" + email + "'" + " and " + COLUMN_PASS + " = " + "'" + pass + "'"; 

     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 
     // Move to first row 
     cursor.moveToFirst(); 
     if (cursor.getCount() > 0) { 

      return true; 
     } 
     cursor.close(); 
     db.close(); 

     return false; 
    } 


    // method for inserting data from method reservation 
    public void addReservation(String name, String surname, String address, String phone, String start, String destination) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     values.put(COLUMN_NAME, name); 
     values.put(COLUMN_SURNAME, surname); 
     values.put(COLUMN_ADDRESS, address); 
     values.put(COLUMN_PHONE, phone); 
     values.put(COLUMN_START, start); 
     values.put(COLUMN_DESTINATION, destination); 

     long a = db.insert(RES_TABLE, null, values); 
     db.close(); 

     Log.e(TAG, "Data insetred" + a); 
    } 


    //Get data from Reservation 
    public boolean getData(String name, String surname, String address, String phone, String start, String destination) { 
     String query = "select * from " + RES_TABLE + " where " + 
       COLUMN_NAME + " = " + "'" + name + "'" + " , " + COLUMN_SURNAME + " = " + "'" + surname + "'" + 
       COLUMN_ADDRESS + " = " + "'" + address + "'" + " , " + COLUMN_PHONE + " = " + "'" + phone + "'" + 
       COLUMN_START + " = " + "'" + start + "'" + " , " + COLUMN_DESTINATION + " = " + "'" + destination + "'"; 

     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(query, null); 
     // Move to first row 
     cursor.moveToFirst(); 
     if (cursor.getCount() > 0) { 

      return true; 
     } 
     cursor.close(); 
     db.close(); 

     return false; 
    } 

    //insert data iinto Reservation 
    public boolean insertReservation(String name, String surname, String address, String phone, String start, String destination) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put("name",name); 
     contentValues.put("surname",surname); 
     contentValues.put("address",address); 
     contentValues.put("phone",phone); 
     contentValues.put("start",start); 
     contentValues.put("destination",destination); 
     db.insert("reservation",null,contentValues); 
     return true; 

    } 
} 

Reservation.java

package com.example.demir.carsharing; 

import android.content.Intent; 
import android.database.SQLException; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import static com.example.demir.carsharing.R.id.etName; 

public class ReservationActivity extends AppCompatActivity { 
    private Button save; 
    private EditText name, surname, address, phone, start, destination; 
    private DbHelper db; 

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

     db = new DbHelper(this); 
     name = (EditText) findViewById(etName); 
     surname = (EditText) findViewById(R.id.etSurname); 
     address = (EditText) findViewById(R.id.etAddress); 
     phone = (EditText) findViewById(R.id.etPhone); 
     start = (EditText) findViewById(R.id.etStart); 
     destination = (EditText) findViewById(R.id.etDestination); 
     save = (Button) findViewById(R.id.btnSave); 
     AddData(); 

    } 

    public void AddData(){ 
     save.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
      boolean isInserted = db.insertReservation(name.getText().toString(), 
         surname.getText().toString(), 
         address.getText().toString(), 
         phone.getText().toString(), 
         start.getText().toString(), 
         destination.getText().toString()); 
       if(isInserted==true) 
        Toast.makeText(ReservationActivity.this,"Data inserted",Toast.LENGTH_LONG).show(); 
        else 
        Toast.makeText(ReservationActivity.this,"Data not inserted",Toast.LENGTH_LONG).show(); 
      } 
     }); 
    } 

} 
+0

後でテーブルにコードを追加しましたか?古いバージョンのデータベースを取り除くためにあなたのアプリをアンインストールしてください。 – laalto

+0

ええと、まずデータベースと1つのテーブルを作成した後、私はseconテーブルを作成しました。 2番目のテーブルを追加するまでは、すべてがうまくいきました。これは、基本的にはADD、UPDATE、DELETを実行しています。 – yimy

答えて

0

タブの作成後にヘルパークラスを変更した可能性がありますあなたが再実行する前にアプリケーションのデータをクリアする必要がある場合は、デバイス/エミュレータでApp Infoに移動し、> Clear Marshmallow for App Info - > Storage - > Clear Data " あなたは、アプリケーション全体を削除して再インストールする必要はありません。それは時間の無駄でしょう。

関連する問題