调用数据库帮助程序类时,应用程序不断停止

时间:2018-10-09 20:45:21

标签: android sqlite android-sqlite

这是我的databaseHelper类

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

import static android.R.attr.key;
import static android.os.Build.VERSION_CODES.N;


public class DatabaseHelper extends  SQLiteOpenHelper{

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "SADER";
    private static final String TABLE_NOTES = "notes";
    private static final String KEY_ID = "id";
    private static final String KEY_TITLE = "title";
    private static final String KEY_CONTENT = "content";

    SQLiteDatabase db;

    public DatabaseHelper(Context context){
        super(context,DATABASE_NAME,null, DATABASE_VERSION);
        db = getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_NOTES_TABLE = "CREATE TABLE " + TABLE_NOTES + " ( "
                + KEY_ID + " INTEGER PRIMARY KEY, "
                + KEY_TITLE + " TEXT, "
                +KEY_CONTENT + " TEXT, "
                + ")";
        db.execSQL(CREATE_NOTES_TABLE);
    }

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

每次我用DatabaseHelper db = new DatabaseHelper(this);来上课时, 该应用程序已停止!!

我该怎么办,在哪里可以找到错误?

2 个答案:

答案 0 :(得分:1)

您要在CREATE TABLE语句字符串中添加while (studentnumber != -1) { cout<<"Enter student number:"<<endl; cin>> studentnumber; numberofstudents ++; cout<<"Gender (1=male, 2=female):"<<endl; cin>> gender; if (gender==1){ gendermale ++; } else { genderfemale++; } cout<<"Age:"<<endl; cin>> Age; average += Age/numberofstudents; cout<<"Program of study:"<<endl; cin>> prgstudy; if (prgstudy=="COEN"){ prgstudycoen ++; } else prgstudyelec++; } cout<<"Statistics:"<<endl; cout<<"Total number of students is: "<<numberofstudents<<endl; cout<<"Number of male students is: "<<gendermale<<endl; cout<<"Number of female students is: "<<genderfemale<<endl; cout<<"Average age of students is:"<<average<<endl; cout<<"Number of COEN students:"<<prgstudycoen<<endl; cout<<"Number of ELEC students:"<<prgstudyelec<<endl; 的额外字符:

,

删除它,它将变成:

String CREATE_NOTES_TABLE = "CREATE TABLE " + TABLE_NOTES + " ( "
        + KEY_ID + " INTEGER PRIMARY KEY, "
        + KEY_TITLE + " TEXT, "
        +KEY_CONTENT + " TEXT, "
        + ")";

为最小化将来的错误,请添加String CREATE_NOTES_TABLE = "CREATE TABLE " + TABLE_NOTES + " ( " + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_TITLE + " TEXT, " +KEY_CONTENT + " TEXT " + ")"; 来检查您的字符串是否正确,如下所示:

Log.d

答案 1 :(得分:1)

总是用try / catch包围以隔离错误。一般是一些SQL语法错误

try {

    DatabaseHelper db = new DatabaseHelper(this);
} catch (Exception e) {
    Log.d("DatabaseHelper", e.getMessage());
}