Android Studio - SQL数据库/表未创建 - 应用程序无法打开

时间:2017-11-14 07:05:20

标签: java android sqlite android-emulator

在Android Studio中创建SQL数据库时,应用程序不会在模拟器上打开,并且在使用设备监视器时,在/ data / data / package / databases /中找不到数据库文件。

我正在研究api_21模拟器,我认为应该支持sqlite。最终我将使用数据库信息来执行按钮的各种功能,但截至目前,这些都被忽略了。

请帮忙!

MainActivity.java

package edu.tamu.thecaddyapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

Button buttonDriver, buttonFouriron, buttonFiveiron, buttonSixiron, buttonSeveniron, buttonEightiron, buttonNineiron, buttonPW, buttonFiftysix, buttonSW, buttonSixty;

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

    SQLiteHelper db = new SQLiteHelper(this);
    db.getWritableDatabase();

    buttonDriver = findViewById(R.id.button_DRIVER);
    buttonFouriron = findViewById(R.id.button_4IRON);
    buttonFiveiron = findViewById(R.id.button_5IRON);
    buttonSixiron = findViewById(R.id.button_6IRON);
    buttonSeveniron = findViewById(R.id.button_7IRON);
    buttonEightiron = findViewById(R.id.button_8IRON);
    buttonNineiron = findViewById(R.id.button_9IRON);
    buttonPW = findViewById(R.id.button_PW);
    buttonSW = findViewById(R.id.button_SW);
    buttonFiftysix = findViewById(R.id.button_56DEG);
    buttonSixty = findViewById(R.id.button_60DEG);

}

public void buttonDriver(View view) {
}

public void buttonFouriron(View view) {
}

public void buttonFiveiron(View view) {
}

public void buttonSixiron(View view) {
}

public void buttonSeveniron(View view) {
}

public void buttonEightiron(View view) {
}

public void buttonNineiron(View view) {
}

public void buttonPW(View view) {
}

public void buttonSW(View view) {
}

public void buttonFiftysix(View view) {
}

public void buttonSixty(View view) {
}

}

SQLiteHelper.java

package edu.tamu.thecaddyapp;

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

public class SQLiteHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "SQLiteDatabase.db";
public static final String TABLE_NAME = "golf_averages";
public static final String _id = "_id";
public static final String COL_1 = "COL_1";
public static final String COL_2 = "COL_2";
public static final String COL_3 = "COL_3";
public static final String COL_4 = "COL_4";
public static final String COL_5 = "COL_5";
public static final String COL_6 = "COL_6";
public static final String COL_7 = "COL_7";
public static final String COL_8 = "COL_8";
public static final String COL_9 = "COL_9";
public static final String COL_10 = "COL_10";

public String create_table = "CREATE TABLE" + TABLE_NAME
        + " (_id integer primary key autoincrement, "
        + COL_1 + "text, "
        + COL_2 + "text, "
        + COL_3 + "text, "
        + COL_4 + "text, "
        + COL_5 + "text, "
        + COL_6 + "text, "
        + COL_7 + "text, "
        + COL_8 + "text, "
        + COL_9 + "text, "
        + COL_10 + "text" +")";


public SQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    Log.d("SQLiteHelper", "Context Reached");
}


@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL(create_table);
    Log.d("onCreate", "onCreate reached");
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS golf_averages");
    onCreate(sqLiteDatabase);
}
}

2 个答案:

答案 0 :(得分:3)

您必须在SQL语句中添加空格。你有以下几行:

+ COL_9 + "text, "

COL_9定义为:

public static final String COL_9 = "COL_9";

意味着SQL语句在编译时最终会使用COL_9并在名称中加入文本,这意味着行名称为COL_9text

在编写text之前添加一个空格,它将修复它。问题不在于第一篇文章(使用ID),它似乎只适用于文本列。

此外,在SQL语句的开头会出现同样的问题:

"CREATE TABLE" + TABLE_NAME

在单词TABLE之后添加空格。

发现这种情况的最简单方法是在logcat中查找错误。这将向您显示SQL语法错误发生的位置,并有助于缩小您需要进行的更改。

答案 1 :(得分:1)

在查询中添加正确的间距,请

public String create_table = "CREATE TABLE " + TABLE_NAME
    + " (_id integer primary key autoincrement, "
    + COL_1 + " text, "
    + COL_2 + " text, "
    + COL_3 + " text, "
    + COL_4 + " text, "
    + COL_5 + " text, "
    + COL_6 + " text, "
    + COL_7 + " text, "
    + COL_8 + " text, "
    + COL_9 + " text, "
    + COL_10 + " text" +")";

在SQL查询中间距非常重要