2012-11-10 94 views
1

我是SQLite的新手,我正在嘗試着去創建一個用於學習目的的基礎數據庫。Android SQLite數據庫onCreate錯誤

在我的主,我宣佈一個新的DbHelper和SQLiteDatabase ..

// Open Database 
    DbHelper dbHelper = new DbHelper(Main.this);   
    SQLiteDatabase db = dbHelper.getWritableDatabase(); 

我DbHelper類..

public class DbHelper extends SQLiteOpenHelper 
{ 
private static final String TAG = "DbHelper"; 

public static final String DB_NAME = "exerciseDB"; 
public static final int DB_VERSION = 1; // User defined - up to you 
public static final String TABLE = "Exercises"; 

// Try keep column names consistent with object names 
public static final String C_ID = "_id"; // "_id" special 
public static final String C_PARENTEXERCISE = "parentExercise"; 
public static final String C_SETNO = "setNo"; 
public static final String C_REPSANDWEIGHT = "repsAndWeight"; 

public String sql; 

Context context; 

public DbHelper(Context context) 
{ 
    super(context, DB_NAME, null, DB_VERSION); 
} 

@Override 
public void onCreate(SQLiteDatabase db) 
{ 
    // SQL commands 
    sql = String.format(
      "CREATE TABLE %s (%s INT PRIMARY KEY %s TEXT %s INT %s TEXT);", 
      TABLE, C_ID, C_PARENTEXERCISE, C_SETNO, C_REPSANDWEIGHT); 

    // Prints out sql String to Logcat 
    Log.d(TAG, "onCreate sql: " + sql); 

    db.execSQL(sql); // Executes the SQL commands 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{ 
    // Maintain users data first 
    db.execSQL("drop table if exists " + TABLE); // Deletes table if it 
                // exists 

    Log.d(TAG, "onUpdata dropped table" + TABLE); 

    // Recreate database 
    this.onCreate(db); 
} 

public String getSQLcmd() 
{ 
    return sql;  
} 

}

SQL命令的輸出logcat中是我相信它應該是..

onCreate sql: CREATE TABLE Exercises (_id INT PRIMARY KEY parentExercise TEXT setNo INT repsAndWeight TEXT); 

logcat中也說:

11-10 17:44:14.702: E/SQLiteLog(13053): (1) near "parentExercise": syntax error 

但我似乎無法看到什麼錯什麼parentExercise有關。

任何幫助將不勝感激。

乾杯。

+3

添加逗號(',')在SQL'String'列之間。 – Luksprog

+0

太簡單了,謝謝! – mgibson

回答

2

查詢應該像

CREATE TABLE Exercises (_id INT PRIMARY KEY, parentExercise TEXT, setNo INT, repsAndWeight TEXT) 
+0

甜美的工作!乾杯! – mgibson

+0

不要忘記接受答案:) –