1

我是 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 相关的任何问题。

任何帮助将不胜感激。

干杯。

4

1 回答 1

2

您的查询应该看起来像

CREATE TABLE Exercises (_id INT PRIMARY KEY, parentExercise TEXT, setNo INT, repsAndWeight TEXT)
于 2012-11-10T18:11:17.847 回答