mgibson
mgibson

Reputation: 6213

Android SQLite Database onCreate error

I am new to SQLite and am trying to go about creating a basic database for learning purposes.

In my Main I declare a new DbHelper and SQLiteDatabase..

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

My DbHelper class..

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;     
}

}

The LogCat output of the sql command is as I believe it should be..

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

LogCat also says:

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

But I can't seem to see anything wrong with anything parentExercise related.

Any help would be greatly appreciated.

Cheers.

Upvotes: 1

Views: 1116

Answers (1)

Mohsin Naeem
Mohsin Naeem

Reputation: 12642

your query should look like

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

Upvotes: 2

Related Questions