This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
                                
                                    (20个答案)
                                
                        
                4年前关闭。
            
        

goodmornig,我在Android Studio上创建数据库时遇到问题。代码没有问题,但应用程序显示“不幸的是stoppep”。我怎么解决这个问题?

package com.example.maria.database;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends Activity {
DatabaseHelper myDB;

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}


还有其他活动

package com.example.maria.database;

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

public class DatabaseHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME = "student.db";
public static final String TABLE_NAME = "student_table";
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);
    SQLiteDatabase db = this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table" + TABLE_NAME + "(ID INTEGER PRIMARY KEY        AUTOINCREMENT, NAME TEXT, SURNAME TEXT, MARKS INTEGER)");

}

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

}
}

最佳答案

您错过了查询空间:

db.execSQL("create table" + TABLE_NAME + "(ID INTEGER PRIMARY KEY        AUTOINCREMENT, NAME TEXT, SURNAME TEXT, MARKS INTEGER)");


它应该是:

db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY        AUTOINCREMENT, NAME TEXT, SURNAME TEXT, MARKS INTEGER)");

07-24 13:28