Hello All
I'm stuck on an SQLite problem and have been for the past few day so I
thought I would share it with you.
All I'm trying to do is create an SQLite database to display the data
generated by the use of my app.
I've managed to get the app to send data to a MySQL database (quite
easily) but for some reason I'm finding this really difficult.
I've made a DB helper class which creates the database and adds one
table to it. I would like to create two tables but achieving one table
for now would be great!
public class DBHelper extends SQLiteOpenHelper
{
// Database constants
private static final String DB_NAME="ScoreStore";
private static final int DB_VERSION = 1;
public static final String TABLE_EVENT = "event";
public static final String COL_ENTRYID = "entryID";
public static final String COL_EVENTNAME = "eventName";
public static final String COL_DATE = "date";
public static final String COL_EVENTTYPE = "eventType";
public static final String COL_NUMROUNDS = "numRounds";
public static final String COL_PLAYSPERROUND = "shotsPerRound";
public static final String COL_PLAYER = "player";
public static final String COL_DISTANCE = "distance";
public static final String COL_TOTALSCORE = "totalScore";
public DBHelper(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
// TODO Auto-generated constructor stub
}
/** Database created for the first time */
@Override
public void onCreate(SQLiteDatabase db)
{
String sql = "create table"+TABLE_EVENT+
"(_id integer not null primary key autoincrement,"+
COL_ENTRYID + "integer, "+
COL_EVENTNAME + "varchar, "+
COL_DATE + "date, "+
COL_EVENTTYPE + "varchar, "+
COL_NUMROUNDS + "integer, "+
COL_PLAYSPERROUND + "integer, "+
COL_PLAYER + "varchar, "+
COL_DISTANCE + "varchar, "+
COL_TOTALSCORE + "integer, )";
db.execSQL(sql);
}
/** Updates the database based on version id */
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion)
{
}
}
Then when I want to add data to the DB I use this code...
DBHelper dbHelper = new DBHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
//Load data
Cursor cursor = db.query(DBHelper.TABLE_EVENT, null, null,
null, null, null, null);
startManagingCursor(cursor);
// populate the database
ContentValues values = new ContentValues();
values.put(DBHelper.COL_EVENTNAME, "Test name");
values.put(DBHelper.COL_DATE, "Today");
values.put(DBHelper.COL_EVENTTYPE, "Test type");
values.put(DBHelper.COL_NUMROUNDS, "1");
values.put(DBHelper.COL_PLAYSPERROUND, "4");
values.put(DBHelper.COL_PLAYER, "Tester");
values.put(DBHelper.COL_DISTANCE, "100 yards");
values.put(DBHelper.COL_TOTALSCORE, "45");
db.insert(DBHelper.TABLE_EVENT, null, values);
// Set the list adapter
String[]from = {DBHelper.COL_ENTRYID,
DBHelper.COL_EVENTNAME,
DBHelper.COL_DATE, DBHelper.COL_EVENTTYPE,
DBHelper.COL_NUMROUNDS,
DBHelper.COL_PLAYSSPERROUND,
DBHelper.COL_PLAYERS,
DBHelper.COL_DISTANCE,
DBHelper.COL_TOTALSCORE};
int[]to = {R.id.the name of my textview};
// Put retrieved data into a list view
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, cursor, from, to);
setListAdapter(adapter);
registerForContextMenu(getListView());
The app runs fine until I try any of the above, then it just crashes.
2 1/2 Questions...
What am I doing wrong? Is what I'm doing hopelessly, completely
incorrect?
Once I've fixed this, how do I make it work for two tables?
Any help would be very much appreciated.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en