简体   繁体   English

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

[英]Android Studio - SQL database/table not creating - app won't open

Working on creating a SQL database in Android Studio, however, the app won't open on the emulator and no database file is found at /data/data/package/databases/ when using the device monitor. 但是,在Android Studio中创建SQL数据库时,该应用程序将无法在模拟器上打开,并且在使用设备监视器时在/ data / data / package / databases /上找不到数据库文件。

I'm working on api_21 emulator which I believe should support sqlite. 我正在研究api_21模拟器,我认为它应该支持sqlite。 Eventually I'll be using the database information to perform various function with the buttons, but as of right now, those are ignored. 最终,我将使用数据库信息来通过按钮执行各种功能,但到目前为止,这些功能已被忽略。

please help! 请帮忙!

MainActivity.java 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 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);
}
}

You have to add spaces in the SQL statement. 您必须在SQL语句中添加空格。 You have these lines: 您有以下几行:

+ COL_9 + "text, "

but COL_9 is defined as: COL_9定义为:

public static final String COL_9 = "COL_9";

meaning the SQL statement, when compiled, ends up with COL_9 and text joined in the name, meaning the row name is COL_9text . 表示SQL语句在编译时以COL_9结尾,并在名称中加入text,表示行名称为COL_9text

Add a space before you write text and it'll fix it. 在编写text之前添加一个空格,它将解决该问题。 The issue isn't there on the first piece though (with ID), it appears to only be for the text columns. 但是,第一部分(带有ID)并不存在此问题,它似乎仅针对文本列。

In addition, the same issue occurs at the start of the SQL statement: 此外,在SQL语句的开头也会发生相同的问题:

"CREATE TABLE" + TABLE_NAME

Add a space after the word TABLE . TABLE单词后面添加一个空格。

The easiest way to spot this is happening, is to look for errors in the logcat. 发现这种情况的最简单方法是在logcat中查找错误。 This shows you where your SQL syntax errors occur, and helps narrow down the changes you need to make. 这将向您显示SQL语法错误的发生位置,并有助于缩小需要进行的更改。

Add Proper spacing in the query, please 请在查询中添加适当的间距

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" +")";

Spacing is very important in SQL Queries 间距在SQL查询中非常重要

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM