this.getWritableDatabase()导致我的Android应用程序停止

时间:2017-09-13 15:00:17

标签: java android sqlite

我是Android SQLite数据库的新手。执行this.getWritableDatabase()我的应用程序停止。有没有解决这个问题的方法?我的数据库是关于让学生和#39;信息并将其标记保存在数据库中。

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME="Dictionary.db";
    public static final String TABLE_NAME="Dictionary_data";
    public static final String COL_1="ID";
    public static final String COL_2="Name";
    public static final String COL_3="SURNAME";
    public static final String COL_4="MARKS";

    public DatabaseHelper(Context context) {
        super(context,DATABASE_NAME,null,1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE"+ TABLE_NAME+ "(ID INTEGER PRIMARY KEY AUTO INCREMENT,NAME TEXT,SURENAME TEXT,MARKS INTEGER)");
    }

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

    public boolean insert_data(String name,String surname,String marks) {
        SQLiteDatabase db =  this.getWritableDatabase();
        ContentValues contentValues= new ContentValues();
        contentValues.put(COL_2,name);
        contentValues.put(COL_3,surname);
        contentValues.put(COL_4,marks);
        long result = db.insert(TABLE_NAME,null,contentValues);

        if(result==-1)
            return false;
        else
            return true;
    }
}

我的mainActivity课程:

public class MainActivity extends AppCompatActivity {

    DatabaseHelper myDB;
    EditText editName,editSurname,editMark;
    Button btn_add_data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDB = new DatabaseHelper(this.getApplicationContext());

        editName = (EditText)findViewById(R.id.namespace);
        editSurname = (EditText)findViewById(R.id.surnamespace);
        editMark = (EditText)findViewById(R.id.markspace);
        btn_add_data = (Button)findViewById(R.id.add_button);
        addData();
    }

    public void addData(){
        btn_add_data.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean id_inserted = myDB.insert_data(editName.getText().toString(),editSurname.getText().toString(),editMark.getText().toString());

                    if(id_inserted)
                        Toast.makeText(MainActivity.this,"Data inserted",Toast.LENGTH_LONG).show();
                    else
                        Toast.makeText(MainActivity.this,"Data not inserted",Toast.LENGTH_LONG).show();
                }
            }
        );
    }
}

1 个答案:

答案 0 :(得分:1)

创建表格中存在错误

db.execSQL("CREATE TABLE"+ TABLE_NAME+ "(ID INTEGER PRIMARY KEY AUTO INCREMENT,NAME TEXT,SURENAME TEXT,MARKS INTEGER)");

请在TABLE

之后添加空格
db.execSQL("CREATE TABLE "+ TABLE_NAME+ " (ID INTEGER PRIMARY KEY AUTO INCREMENT,NAME TEXT,SURENAME TEXT,MARKS INTEGER)");

还添加了log cat以获取更多详细信息,我无法发表评论,因为这是我的新帐户。