1

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.

1
  • 3
    Add commas(,) between the columns in the sql String. Commented Nov 10, 2012 at 18:09

1 Answer 1

2

your query should look like

CREATE TABLE Exercises (_id INT PRIMARY KEY, parentExercise TEXT, setNo INT, repsAndWeight TEXT)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.