繁体   English   中英

Android Studio 创建数据库错误(android.database.sqlite.SQLiteException: near "and": syntax error (code 1): )详解

[英]Android Studio Create Database error (android.database.sqlite.SQLiteException: near "and": syntax error (code 1): ) Detailed Analysis

我正在尝试使用数据库,但它抱怨我的“创建表”。
我查看了不同的帖子并“在此处更新了我对 CREATE TABLE 所做的更改”。
与其他帖子和论坛相比,它看起来是正确的,所以我不太确定为什么它总是失败。
当我尝试在主活动中调用“填充游戏数据库”方法时,它似乎只会崩溃。
当对象刚创建时,没有问题。
我读了一些关于“一旦创建数据库然后缓存它”的内容,因此它不需要再次发生。
所以我想我必须重新制作数据库,因为(也许)数据库已经缓存了?
但是,在创建对象时,会自动执行 CREATE TABLE 语句,因此我不确定为什么在调用填充数据库的方法时会出错。

主要活动类

package tekvision.codedecrypter;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
import gameInfo.GameDatabase;


public class MainActivity extends ActionBarActivity {

//Runs before the application is created
public Button mCampaignButton;


//When the application is created
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //
    // I wanted to call it heat and use it in a toast to make sure its   working
    //Gamedatabase. does not work to find my method

   //


   //Keeps screen on so it doesn't fall asleep
   getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

  //Finding button by button id after application is created
  mCampaignButton = (Button)findViewById(R.id.campaignButtonID);

   //Checks if the campaign button is clicked
   mCampaignButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {

        //Toast pop up message
        Toast toast = Toast.makeText(getApplicationContext(),
            "campaign select",
                Toast.LENGTH_SHORT);

        toast.show();


        //Intent to go from main activity to campaign Level Select Activity
        Intent intent = new Intent(MainActivity.this, CampaignSelectLevel.class);
        startActivity(intent);


    }
});

}}

游戏数据库类

package gameInfo;

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

/**
* Created by User on 06/06/2015.
*/

//Extends the sql database open helper it will be error until the 2 methods     are added plus the constructor
//the database is saved to a text file
public class GameDatabase extends SQLiteOpenHelper {

//Version number of the database
//Every update to the database will result in going up in the database version number
private static final int DATABASE_VERSION = 1;

//Private set of final strings, for the column names in the database
private static final String DATABASE_NAME =
    "Database",
    TABLE_1 = "Answers and Hints",
    TABLE_2 = "classical",
    TABLE_3 = "ancient",
    KEY_ID = "id",
    KEY_HINT = "hint",
    KEY_ANSWER = "answer",
    KEY_QUESTION = "question",
    KEY_INFO = "info",
    KEY_IMAGE = "image";




  //Database Constructor, sets the databases named and the version of the database
  public GameDatabase(Context context){

super(context, DATABASE_NAME, null, DATABASE_VERSION);
}


  //Whenever database is created
 //Creating a table with each column and specify each columns type such as text or integer that is the primary key
 @Override
 public void onCreate(SQLiteDatabase db){
 db.execSQL("CREATE TABLE " + TABLE_1 + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_QUESTION + " TEXT" + KEY_ANSWER + " TEXT" + KEY_IMAGE + "IMAGEVIEW" + KEY_HINT + " TEXT" + KEY_INFO + " TEXT)");

}

//When the database is upgraded
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("DROP TABLE IF EXISTS " + TABLE_MODERN);

onCreate(db);
}




 //Currently should have ONE row for level on in modern
 public void fillGameDatabase(){
    SQLiteDatabase db = getWritableDatabase();

    ContentValues values = new ContentValues();

    //Fills information for the first row by a few columns

    //Modern ERA
    values.put(KEY_QUESTION, "US President");
    values.put(KEY_ANSWER, "Barack Obama");
    values.put(KEY_HINT, "He is the first African American president");
    values.put(KEY_INFO, "Barack Obama is the 44th President of the United States, and the first African American to hold the office. Born in Honolulu, Hawaii, Obama is a graduate of Columbia University and Harvard Law School, where he served as president of the Harvard Law Review. He was a community organizer in Chicago before earning his law degree. He worked as a civil rights attorney and taught constitutional law at University of Chicago Law School from 1992 to 2004. He served three terms representing the 13th District in the Illinois Senate from 1997 to 2004, running unsuccessfully for the United States House of Representatives in 2000.");
    values.put(KEY_IMAGE, "R.drawable.obama.jpg");
    db.insert(TABLE_MODERN, null, values); //inserted a new row into the    database

    db.close();

 }


 //Gets the answers based on the era nd level provided,
 //db is database extension dont need to pass it
 public Cursor getAnswer(String table, int level){
 SQLiteDatabase db = getReadableDatabase();

 Cursor cursor;

//All one row of data
String[] projections = {KEY_QUESTION, KEY_ANSWER, KEY_HINT, KEY_INFO, KEY_IMAGE};

//Calling query method
//Pass table name, the projections(names of columns), selection (data argument), selection arguments, group rows
//filter by row groups, sort order, you can pass null for ones you dont want to enter
cursor = db.rawQuery("SELECT " + KEY_ANSWER + " FROM " + TABLE_MODERN + " WHERE " + KEY_ID + "=" + level, null);

db.close();
return cursor;
}
}

您的表名包含空格...因此您应该将其括在方括号中,例如[Answers and Hints]
或者,更好的是使用下划线,就像这样Answers_and_Hints

否则,SQLite DDL(数据定义语言)会将不同的词解释为不同的命令。

暂无
暂无

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

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