我是SQLite的新手,我正在尝试为学习目的创建基本数据库。
在我的Main中,我声明了一个新的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有关的错误。
非常感谢任何帮助。
干杯。
答案 0 :(得分:2)
您的查询应该是
CREATE TABLE Exercises (_id INT PRIMARY KEY, parentExercise TEXT, setNo INT, repsAndWeight TEXT)