Reformatting the table ddl


#1

Since I have a bit of SQL under my belt, I redid the table creates in RunDatabaseHelper like this:

public class RunDatabaseHelper extends SQLiteOpenHelper {
...
	private static final String TABLE_RUN = "run";
	private static final String COLUMN_RUN_START_DATE = "startdate";
   private static final String TABLE_RUN_DDL = 
		"create table run (" +
			" _id integer primary key autoincrement " +
			",startdate integer" +
		")"
	;
	
	private static final String TABLE_LOCATION = "location";
   private static final String COLUMN_LOCATION_LATITUDE = "latitude";
   private static final String COLUMN_LOCATION_LONGITUDE = "longitude";
   private static final String COLUMN_LOCATION_ALTITUDE = "altitude";
   private static final String COLUMN_LOCATION_TIMESTAMP = "timestamp";
   private static final String COLUMN_LOCATION_PROVIDER = "provider";
   private static final String COLUMN_LOCATION_RUN_ID = "run_id";

	private static final String TABLE_LOCATION_DDL = 
		"create table location (" +
			" timestamp integer" +
			",latitude real" +
			",longitude real" +
			",altitude real" +
			",provider varchar(100)" +
			",run_id integer references run(_id)" +
		")"
	;
...

then, in onCreate, I only have:

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(TABLE_RUN_DDL);
		db.execSQL(TABLE_LOCATION_DDL);
	}

I think it’s a little neater and keeps the SQL nicely formatted. Note the use of the convention to start continued SQL lines with a comma. That makes it easier to insert or delete new lines without worrying about the punctuation.


#2

:smiley:

I approve! SQL formatting is a pet peeve of mine - I can’t stand seeing something unreadable in code. I don’t think ours is that bad, but we did elect to compress it a bit for the sake of space.


#3

In addition, I think you can change to


    private static final String TABLE_RUN = "run";
    private static final String COLUMN_RUN_ID = "_id";
    private static final String COLUMN_RUN_START_DATE = "startdate";

    private static final String TABLE_RUN_DDL = 
      "CREATE TABLE " + TABLE_RUN + " (" +
          COLUMN_RUN_ID +  " INTEGER PRIMARY KEY AUTOINCREMENT, " +
          COLUMN_RUN_START_DATE + " INTEGER" + ")";

in case you change the table name or column name