2016-06-19 65 views
-1

創建DbManger類來處理SQLite數據庫操作,我嘗試插入數據庫中的值它給出一個錯誤SQLite的給我附近的「創建」的錯誤:語法錯誤(代碼1)

E/SQLiteDatabase( 6729):android.database.sqlite.SQLiteException:near「CREATE」:syntax error(code 1):,while compiling:INSERT INTO CREATE TABLE relations(_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT NOT NULL,email TEXT NOT NULL,age TEXT NOT NULL,位置TEXT NOT NULL);(電子郵件,姓名,年齡,位置)VALUES(,,,)

DbManager類

????
public class DBManager { 

    int rowsAffected; 

    // Database and version 
    private static final String DB_NAME = "familyHistory"; 
    private static final int DB_VERSION = 1; 

    private static final String TABLE_INFO = "familyTable"; 

    private static final String _ROWID = "_id"; 
    private static final String _NAME = "name"; 
    private static final String _EMAIL = "email"; 
    private static final String _AGE = "age"; 
    private static final String _LOCATIONs = "location"; 

    private static final String CREATE_PRO = "CREATE TABLE " + TABLE_INFO + "(" + 
      _ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
      _NAME + " TEXT NOT NULL, " + 
      _EMAIL + " TEXT NOT NULL, " + 
      _AGE + " TEXT NOT NULL, " + 
      _LOCATIONs + " TEXT NOT NULL);"; 

    private final Context ourContext; 
    private static DBHelper ourDBHelper; 
    private SQLiteDatabase ourDatabase; 

    public DBManager(Context ctx) { 
     this.ourContext = ctx; 
     Log.i("db", "DBManager(Context ctx)"); 
    } 

    private static class DBHelper extends SQLiteOpenHelper { 

     public DBHelper(Context context) { 
      // SQLiteOpenHelper Constructor Creating database 
      super(context, DB_NAME, null, DB_VERSION); 
      Log.i("db", "DBHelper(Context context)"); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      db.execSQL(CREATE_PRO); 
      Log.e(">>>>>", CREATE_PRO); 
      // db.execSQL(CREATE_TRVLBOK); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) { 
      Log.w("Nomad", "Upgrading database from version " + oldVer + " to " + newVer 
        + ", which will destroy all old data"); 
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_INFO); 
      // db.execSQL("DROP TABLE IF EXISTS " + TABLE_TRAVELBOK); 
      onCreate(db); 
      Log.i("db", "onUpgrade)"); 
     } 

    } 

    public DBManager open() throws SQLException { 
     ourDBHelper = new DBHelper(ourContext); 
     ourDatabase = ourDBHelper.getWritableDatabase(); 
     Log.i("db", "open()"); 
     return this; 
    } 

    public void close() { 
     ourDBHelper.close(); 
     Log.i("db", "close()"); 
    } 



    public long insertFamRec(String UserName, String Email, String age, String location) { 
     ContentValues cv = new ContentValues(); 
     cv.put(_NAME, UserName); 
     cv.put(_EMAIL, Email); 
     cv.put(_AGE, age); 
     cv.put(_LOCATIONs, location); 
     Log.i("db", "insertLocRec"); 
     return ourDatabase.insert(CREATE_PRO, null, cv); 

    } 
} 

而且從插入類我添加了這行,但仍錯誤

DBManager db = new DBManager(NewRelation.this); 
    db.open(); 
    long i = db.insertFamRec("strfullName", "stremail", "strage", "strlocation"); 

    db.close(); 
    if (i > 0) { 
     ShowMessage.Message(NewRelation.this, "data is added"); 
    } else { 
     Toast.makeText(getApplicationContext(), "data is failed", Toast.LENGTH_SHORT).show(); 
    } 

如果有人給下來的問題,請提到它爲什麼評級。

回答

1

這個問題似乎是您撥打ourDatabase.insert的地方,您將查詢字符串傳遞給表名的位置。你希望用這條線實現什麼:return ourDatabase.insert(CREATE_PRO, null, cv);

行更改爲:

return ourDatabase.insert(TABLE_INFO, null, cv); 

注意,而不是CREATE_PRO,我通過TABLE_INFO這是要插入到表的名稱。 我希望這可以解決您的問題。請讓我知道它是否有幫助。

+0

謝謝@ishmael我忘了!複製粘貼的缺點:P :) – Attaullah

+0

複製粘貼可能代價高昂!我很高興能夠提供幫助,如果您覺得有用,請回答投票。 – ishmaelMakitla

相關問題