2011-02-05 58 views
1

當我開始了我的應用程序,我得到啓動時出錯:android.database.sqlite.SQLiteException:near「)」:語法錯誤:CREATE TABLE table1(id INTEGER PRIMARY KEY,name TEXT));

02-05 18:50:14.943: ERROR/AndroidRuntime(328): Caused by: android.database.sqlite.SQLiteException: near ")": syntax error: CREATE TABLE table1(id INTEGER PRIMARY KEY, name TEXT)); 

這是我主要的.java:

package com.shoppinglist; 

import android.app.Dialog; 
import android.app.ListActivity; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 

public class ShoppingList extends ListActivity { 

    private DataHelper DataHelper; 
    /** Called when the activity is first created. */ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     DataHelper = new DataHelper(this); 

     ListView listview = (ListView) findViewById(android.R.id.list); 

     Cursor c = (Cursor) DataHelper.selectAll(); 
     startManagingCursor(c); 

     String[] from = new String[] { DataHelper.getDatabaseName() }; 
     int[] to = new int[] { android.R.id.list }; 

     ListAdapter adapter = new SimpleCursorAdapter(this, 
       R.layout.rowlayout, c, from, to); 
     setListAdapter(adapter); 

     Button button1main = (Button) findViewById(R.id.add); 
     button1main.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
      final Dialog additem = new Dialog(ShoppingList.this); 
      additem.setContentView(R.layout.maindialog); 
      final EditText et = (EditText)additem.findViewById(R.id.edittext); 
      additem.setTitle("Type your item"); 
      additem.setCancelable(true); 
      et.setHint("Type the name of an item..."); 

      Button button = (Button) additem.findViewById(R.id.cancel); 
      button.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        additem.dismiss(); 
       } 
      }); 
      additem.show(); 

      Button ok = (Button) additem.findViewById(R.id.ok); 
      ok.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        final String text = et.getText().toString(); 
        additem.dismiss(); 
        et.setText(""); 
       } 
      }); 
     } 
     }); 
    } 
} 

這裏是我的DataHelper類:

package com.shoppinglist; 

import java.util.ArrayList; 
import java.util.List; 

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

public class DataHelper { 

     private static final String DATABASE_NAME = "items.db"; 
     private static final int DATABASE_VERSION = 1; 
     private static final String TABLE_NAME = "table1"; 

     private Context context; 
     private SQLiteDatabase db; 

     private SQLiteStatement insertStmt; 
     private static final String INSERT = "insert into " 
      + TABLE_NAME + "(name) values (?)"; 

     public DataHelper(Context context) { 
      this.context = context; 
      OpenHelper openHelper = new OpenHelper(this.context); 
      this.db = openHelper.getWritableDatabase(); 
      this.insertStmt = this.db.compileStatement(INSERT); 
     } 

     public long insert(String name) { 
      this.insertStmt.bindString(1, name); 
      return this.insertStmt.executeInsert(); 
     } 

     public void deleteAll() { 
      this.db.delete(TABLE_NAME, null, null); 
     } 

     public List<String> selectAll() { 
      List<String> list = new ArrayList<String>(); 
      Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name" }, 
      null, null, null, null, "name desc"); 
      if (cursor.moveToFirst()) { 
      do { 
       list.add(cursor.getString(0)); 
      } while (cursor.moveToNext()); 
      } 
      if (cursor != null && !cursor.isClosed()) { 
      cursor.close(); 
      } 
      return list; 
     } 

     public static String getDatabaseName() { 
     return DATABASE_NAME; 
    } 

    private static class OpenHelper extends SQLiteOpenHelper { 

      OpenHelper(Context context) { 
      super(context, getDatabaseName(), null, DATABASE_VERSION); 
      } 

      @Override 
      public void onCreate(SQLiteDatabase db) { 
      db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT));"); 

      } 

      @Override 
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      Log.w("Example", "Upgrading database, this will drop tables and recreate."); 
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
      onCreate(db); 
      } 
     } 
    } 

提前感謝!

+0

兩個人已經回答了你的問題,但你不覺得錯誤信息本身足夠清楚嗎?從什麼時候stackoverflow是一個調試轉儲? – fbernier 2011-02-06 00:11:07

回答

2
db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT);"); 

是你想要的。你有一到多個右括號。

3

在你創建表語句你有一個額外 ')'

嘗試:

db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT);"); 
相關問題