编译时:创建表错误

时间:2014-09-15 10:28:56

标签: java android sql

编译时,此错误显示:

  

android.database.sqlite.SQLiteException:near" TABLEnewtable&#34 ;:语法错误(代码1):,编译时:CREATE TABLEnewtable {id INTEGER PRIMERY KEY,editname TEXT,edittel TEXT,editskype TEXT,editaddress TEXT}。

public class DataManipulator {
    private static final String DATABASE_NAME = "mydatabase.db";
    private static final int DATABASE_VERSION = 1;
    static final String TABLE_NAME = "newtable";
    private static Context context;
    static SQLiteDatabase db;
    private SQLiteStatement insertStmt;
    private static final String INSERT = "insert into" +TABLE_NAME+ "(editname, edittel, editskype, 
    editaddress) values(?,?,?,?)";
    public DataManipulator(Context context)
    {
        DataManipulator.context = context; 
        OpenHelper openHelper = new OpenHelper(DataManipulator.context);
        DataManipulator.db = openHelper.getReadableDatabase();
        this.insertStmt = DataManipulator.db.compileStatement(INSERT);
    }

    public long insert(String editname, String edittel, String editskype, String editaddress)
    {
        this.insertStmt.bindString(1, editname);
        this.insertStmt.bindString(2, edittel);
        this.insertStmt.bindString(3, editskype);
        this.insertStmt.bindString(4, editaddress);
        return this.insertStmt.executeInsert();
    }

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

    public List<String[]> selectAll()
    {
        List<String[]> list = new ArrayList<String[]>();
        Cursor cursor = db.query(TABLE_NAME, new String[]{"id","editname", "edittel", "editskype", 
        "editaddress"},null, null, null, null, "name asc");
        int x=0;
        if(cursor.moveToFirst())
        {
            do {
                String[] bb= new String[] { 
            cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4)};
                list.add(bb);
                x=x+1;
            }
            while(cursor.moveToNext());
        }
        if(cursor != null && !cursor.isClosed())
        {
            cursor.close();
        }
        cursor.close();
        return list;
    }

    public void delete(int rowId)
    {
        db.delete(TABLE_NAME, null, null);
    }

    private static class OpenHelper extends SQLiteOpenHelper
    {
        OpenHelper(Context context)
        {
            super(context,DATABASE_NAME,null,DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL("CREATE TABLE" +TABLE_NAME+ "{id INTEGER PRIMERY KEY, editname TEXT, edittel TEXT, editskype TEXT, editaddress TEXT }");

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS"+ TABLE_NAME);
            onCreate(db);

        }
    }
}

2 个答案:

答案 0 :(得分:4)

您有很多基本的SQL语法问题。请先考虑学习一些基本的SQL和堆栈跟踪。

  1. TABLE等SQL关键字和newtable等标识符之间添加空格。

    例如,更改

    "CREATE TABLE" +TABLE_NAME+
    

     "CREATE TABLE " +TABLE_NAME+
    

     "insert into" +TABLE_NAME+
    

     "insert into " +TABLE_NAME+
    
  2. CREATE TABLE中的括号应为( )而不是{ }

  3. PRIMERY中的错误,应为PRIMARY

  4. 另外可能还有更多;这些只是查看SQL的前10秒内发现的问题。

答案 1 :(得分:0)

检查一下:

db.execSQL("CREATE TABLE " +TABLE_NAME+ "(id INTEGER PRIMARY KEY, editname TEXT, edittel TEXT, editskype TEXT, editaddress TEXT )");