简体   繁体   English

为什么我的应用程序在打开数据库时崩溃?

[英]Why is my app crashing on opening the database?

everyone, I'm a completely new in android app development, and was trying my hand at using database to store some strings, but every time I try to open my database, my app crashes with the following error message: 大家,我是android应用程序开发的一个全新手,正在尝试使用数据库存储一些字符串,但是每次尝试打开数据库时,我的应用程序都会崩溃,并显示以下错误消息:

android.database.sqlite.SQLiteException: near "TABLETASKS": syntax error (code 1): , while compiling: CREATE TABLETASKS(_idinteger primary key autoincrementtasktext not null); android.database.sqlite.SQLiteException:在“ TABLETASKS”附近:语法错误(代码1):,在编译时:CREATE TABLETASKS(_idinteger主键autoincrementtasktext不为null);

Full code and error log follows: 完整的代码和错误日志如下:

package org.example.tasklist;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

HelperMethods localhelpermethods;
OpenHelper localopenhelper;
SQLiteDatabase localdatabase;

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

    Context localcontext = getApplicationContext();

    localopenhelper = new OpenHelper(localcontext);
    localhelpermethods = new HelperMethods(localcontext);

    final ArrayList<String> al = new ArrayList<String>();
    ListView listView1 = (ListView)findViewById(R.id.listView1);
    final EditText editText1 = (EditText)findViewById(R.id.editText1);
    Button button1 = (Button)findViewById(R.id.button1);

    final ArrayAdapter<String> aa = new ArrayAdapter<String>           (this,android.R.layout.simple_list_item_1,al);
    listView1.setAdapter(aa);

    button1.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v) 
        { 
           localhelpermethods.open_database();
           String task = editText1.getText().toString();
           localhelpermethods.add_task(task);
           aa.notifyDataSetChanged();
        }
     });

}


@Override
 public boolean onCreateOptionsMenu(Menu menu) {
     // Inflate the menu; this adds items to the action bar if it is present.
     getMenuInflater().inflate(R.menu.main, menu);
     return true;
 }    
}

my OpenHelper class. 我的OpenHelper类。 package org.example.tasklist; 包org.example.tasklist;

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

public class OpenHelper extends SQLiteOpenHelper
{


  public static final String DATABASE_NAME = "TASKS.db";
  public static final int DATABASE_VERSION = 1;
  public final String DATABASE_TABLE = "TASKS";

  //Columns in the table.
  public final String KEY_TASK = "task";
  public final String KEY_ID = "_id";

  private static Context context;

  public OpenHelper(Context context)
  {
    super(context,DATABASE_NAME,null,DATABASE_VERSION);
    this.context = context;
  }

  public void onCreate(SQLiteDatabase database)
  {
      database.execSQL("CREATE TABLE" + DATABASE_TABLE + "(" + KEY_ID + "integer primary key autoincrement" 
  + KEY_TASK + "text not null);");
  }

  public void onUpgrade(SQLiteDatabase database, int oldversion, int newversion)
  {
     if(newversion > oldversion)
     {
         database.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE );
         onCreate(database);
     }
  }
}

My HelperMethods class. 我的HelperMethods类。

package org.example.tasklist;

import java.util.ArrayList;

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

public class HelperMethods
{
    OpenHelper OH;
    SQLiteDatabase task_database;
    ArrayList<String> return_list = new ArrayList<String>();
    private static Context localcontext;

    public HelperMethods(Context context)
    {
        HelperMethods.localcontext = context;
        OH = new OpenHelper(localcontext);
    }

    public HelperMethods open_database() throws SQLException
    {
        task_database = OH.getWritableDatabase();
        return this;
    }

    public ArrayList<String> getAllTasks()
    { 
        String[] columns = new String[] {OH.KEY_TASK};
        Cursor cursor = task_database.query(OH.DATABASE_TABLE, columns, null, null, null, null, null, null);
        while(cursor.moveToNext())
        {
            return_list.add(cursor.getString(cursor.getColumnIndex(OH.KEY_TASK)));
        }
        return return_list;     
    }

    public void add_task(String task)
    {
        OH.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(OH.KEY_TASK, task);
        task_database.insert(OH.KEY_TASK, null, cv);
        OH.close();
    }

}

logcat error output. logcat错误输出。

05-02 16:08:50.990: E/SQLiteLog(2590): (1) near "TABLETASKS": syntax error
05-02 16:08:51.020: E/AndroidRuntime(2590): FATAL EXCEPTION: main
05-02 16:08:51.020: E/AndroidRuntime(2590): Process: org.example.tasklist, PID: 2590
05-02 16:08:51.020: E/AndroidRuntime(2590): android.database.sqlite.SQLiteException: near "TABLETASKS": syntax error (code 1): , while compiling: CREATE TABLETASKS(_idinteger primary key autoincrementtasktext not null);
05-02 16:08:51.020: E/AndroidRuntime(2590):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
05-02 16:08:51.020: E/AndroidRuntime(2590):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
05-02 16:08:51.020: E/AndroidRuntime(2590):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
05-02 16:08:51.020: E/AndroidRuntime(2590):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
05-02 16:08:51.020: E/AndroidRuntime(2590):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
05-02 16:08:51.020: E/AndroidRuntime(2590):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
05-02 16:08:51.020: E/AndroidRuntime(2590):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
05-02 16:08:51.020: E/AndroidRuntime(2590):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
05-02 16:08:51.020: E/AndroidRuntime(2590):     at org.example.tasklist.OpenHelper.onCreate(OpenHelper.java:29)
05-02 16:08:51.020: E/AndroidRuntime(2590):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
05-02 16:08:51.020: E/AndroidRuntime(2590):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
05-02 16:08:51.020: E/AndroidRuntime(2590):     at org.example.tasklist.HelperMethods.open_database(HelperMethods.java:26)
05-02 16:08:51.020: E/AndroidRuntime(2590):     at org.example.tasklist.MainActivity$1.onClick(MainActivity.java:46)
05-02 16:08:51.020: E/AndroidRuntime(2590):     at android.view.View.performClick(View.java:4424)
05-02 16:08:51.020: E/AndroidRuntime(2590):     at android.view.View$PerformClick.run(View.java:18383)
05-02 16:08:51.020: E/AndroidRuntime(2590):     at android.os.Handler.handleCallback(Handler.java:733)
05-02 16:08:51.020: E/AndroidRuntime(2590):     at android.os.Handler.dispatchMessage(Handler.java:95)
05-02 16:08:51.020: E/AndroidRuntime(2590):     at android.os.Looper.loop(Looper.java:137)
05-02 16:08:51.020: E/AndroidRuntime(2590):     at android.app.ActivityThread.main(ActivityThread.java:4998)
05-02 16:08:51.020: E/AndroidRuntime(2590):     at java.lang.reflect.Method.invokeNative(Native Method)
05-02 16:08:51.020: E/AndroidRuntime(2590):     at java.lang.reflect.Method.invoke(Method.java:515)
05-02 16:08:51.020: E/AndroidRuntime(2590):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
05-02 16:08:51.020: E/AndroidRuntime(2590):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
05-02 16:08:51.020: E/AndroidRuntime(2590):     at dalvik.system.NativeStart.main(Native Method)

You have a typo: _idinteger primary key autoincrementtasktext not null 您有错字:_idinteger主键autoincrementtasktext不为null

should be _idinteger primary key autoincrement tasktext not null 应为_idinteger主键自动递增任务文本,不为null

Note the space 注意空格

You must have to add some space between your table row name and the datatype to identify it ,which you have missed. 您必须在表行名称和数据类型之间添加一些空格以标识它,而您却错过了。

Just change your create table query as below: 只需更改您的创建表查询,如下所示:

database.execSQL("CREATE TABLE " + DATABASE_TABLE + " ( " + KEY_ID + " integer primary key autoincrement " + KEY_TASK + " text not null); ");

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

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