java - SQLite DatabaseHelper 类不创建数据库

标签 java android sqlite android-sqlite

大家好,我一直在开发一个简单的 Chronometer 应用程序,该应用程序将单圈时间存储在数据库中。我创建了一个数据库助手类。当我尝试从我的 Activity 中插入数据时,我收到一条错误消息,指出没有创建这样的表。为了简单起见,我从这里的 Activity 中删除了一些代码(按钮代码等) 起初我虽然使用常量有问题但是我创建了没有任何常量的表并且我得到了同样的错误。
更新:我认为我的 onCreate 从未在 DBHelper 中被调用。

数据库助手.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)

最佳答案

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

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

要解决此更改,请将 + "laps"+ "text not null, " 更改为 + "laps"+ "text not null "(即删除逗号结束。)。

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

关于java - SQLite DatabaseHelper 类不创建数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48158840/

相关文章:

java - 随机生成字符串格式的日期之后的日期

Android WebView 无法正确渲染网页

android - 如何使用 Dagger 异步提供对象?

javascript - android studio 和 sqlite 检查用户名是否已存在

sqlite - 在 SQLite 中存储 bool 值

java - 带有 spring data 的多个数据源 - 需要设置属性 continueOnError

java - 如何在使用 HttpsURLConnection 时覆盖 Android 发送到服务器的密码列表?

java - 如何自定义验证失败返回的字段名称?

android - placeautocomplete 结果代码 = 2

android - 异常无效限制条款 - Android