SQLite DatabaseHelper类不创建数据库

时间:2018-01-08 22:17:58

标签: java android sqlite android-sqlite

您好我一直在研究一个简单的计时器应用程序,它可以在数据库中存储单圈时间。我已经创建了一个Database Helper类。当我尝试从我的活动中插入数据时,我收到一条错误消息,表示没有创建此类表。为简单起见,我已从活动中删除了一些代码(按钮代码等) 起初我虽然使用常量有问题,但我创建了没有任何一个的表,我得到了同样的错误。
更新:我认为我的onCreate永远不会在DBHelper中调用。

DatabaseHelper.java

package com.example.andrei.gymhelp;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class DatabaseHelper extends SQLiteOpenHelper {

private static final String TAG = "SQLMessage";
private static final String DATABASE_NAME = "StoredResults.db";
private static final int DATABASE_VERSION = 2;
private static DatabaseHelper mDatabaseInstance = null;
private Context mContext;


public static DatabaseHelper newInstance(Context context){
    //first check to see if the database helper
    //member data is null
    //create a new one if it is null

    if (mDatabaseInstance == null){
        mDatabaseInstance = new DatabaseHelper(context.getApplicationContext());
    }

    //either way we have to always return an instance of
    //our database class each time this method is called
    return mDatabaseInstance;
}



public DatabaseHelper(Context context) {

    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    mContext = context;
}
private static final String CREATE_TABLE_LAPS = "create table results_table "
        + "("
        + "_id" + " integer primary key autoincrement, "
        + "timestamp" + " text not null, "
        + "laps" + " text not null, "
        + ")";

@Override
public void onCreate(SQLiteDatabase db) {
   try{
       db.execSQL(CREATE_TABLE_LAPS);
       Log.d(TAG, "TABLE WAS CREATED ");
   } catch (SQLException e) {
       Log.d(TAG, " Error create database " + e.getMessage());
   }
}

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

 }
}

TimerActivity.java

public class TimerActivity extends AppCompatActivity {

private Context context;
private TextView timer;
private Button btnStart;
private Button btnLap;
private Button btnStop;
private Button btnSave;
private TextView laps;
private int lapsCounter = 1;
private ScrollView scrollLaps;
private boolean saved = false;
private String savedLaps = "";
private static final String TAG = "MESSAGE";
Date dateAndTime = new Date();
private String sequenceTimestamp;

private DatabaseHelper mDBHelper;
private SQLiteDatabase mDatabase;

private Chronometer mainChronometer;
private Thread threadChrono;

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


    mDBHelper = new DatabaseHelper(this);
    mDatabase  = mDBHelper.getWritableDatabase();

    btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mainChronometer != null) {

                Context context = getApplicationContext();
                CharSequence text = getString(R.string.chrono_not_stopped);
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();

            }else{
                if (!saved) {

                    Log.v(TAG, "LAPS = " + savedLaps);
                    Log.v(TAG, String.valueOf(System.currentTimeMillis()));
                    saved = true;

                    Context context = getApplicationContext();
                    CharSequence text = getString(R.string.saved_toast);
                    int duration = Toast.LENGTH_SHORT;

                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();
                    sequenceTimestamp = (String) DateFormat.format("hh:mm:ss dd-MM-yyyy", dateAndTime.getTime());

                    saveResultsToDatabase(sequenceTimestamp, savedLaps);
                    Log.v(TAG, sequenceTimestamp);

                    } else {
                        Context context = getApplicationContext();
                        CharSequence text = getString(R.string.alreadysaved_toast);
                        int duration = Toast.LENGTH_SHORT;

                        Toast toast = Toast.makeText(context, text, duration);
                        toast.show();
                    }
            }
        }
    });

}

public void updateTimer(final String time){
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            timer.setText(time);
        }
    });
}
public void saveResultsToDatabase(String timestamp,String stringOfLaps) {
    //if (mDatabase != null) {
        //prepare the transaction information that will be saved to the database
        ContentValues values = new ContentValues();
        values.put(Constants.COLUMN_TIMESTAMP, timestamp);
        values.put(Constants.COLUMN_LAPS, stringOfLaps);

        try {


            Log.i(TAG,"SAVE RESULTS WAS CALLED");
            mDatabase.insert(Constants.LAPS_TABLE, null, values);

            mDatabase.close();
        }catch (SQLException e){
            Log.d(TAG,e.getMessage());
        }
    //}
}

}

错误

01-08 22:09:09.081 10656-10656/com.example.andrei.gymhelp E/SQLiteLog: (1) 
01-08 22:09:09.083 10656-10656/com.example.andrei.gymhelp E/SQLiteDatabase: Error inserting timestamp=10:09:04 08-01-2018 laps=Lap 1: 
00:00:00:786|Lap 2: 00:00:01:283|Lap 3: 00:00:01:756|Lap 5: 00:00:02:177|
 android.database.sqlite.SQLiteException: no such table: results_table (Sqlite code 1): , while compiling: INSERT INTO results_table(timestamp,laps) VALUES (?,?), (OS error - 2:No such file or directory)
     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:910)
     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:521)
     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:603)
     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:63)
     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1725)
     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1593)
     at com.example.andrei.gymhelp.TimerActivity.saveResultsToDatabase(TimerActivity.java:186)
     at com.example.andrei.gymhelp.TimerActivity$4.onClick(TimerActivity.java:150)
     at android.view.View.performClick(View.java:5646)
     at android.view.View$PerformClick.run(View.java:22459)
     at android.os.Handler.handleCallback(Handler.java:761)
     at android.os.Handler.dispatchMessage(Handler.java:98)
     at android.os.Looper.loop(Looper.java:156)
     at android.app.ActivityThread.main(ActivityThread.java:6523)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)

1 个答案:

答案 0 :(得分:1)

数据库确实存在。但是 results_table 表不在数据库中。

由于语法错误,未创建表。最后定义的列有一个尾随逗号,因此需要另一个列定义。

要将此更改+ "laps" + " text not null, "修复为+ "laps" + " text not null "(即删除末尾的逗号)。

只有在数据库不存在的情况下才会调用onCreate方法,在重新运行应用程序之前,您应该a)删除应用程序的数据或b)卸载应用程序。