Problem developing app for Android
Hi All,
I am a not experienced developer. All I want to do is make a small app for an Android phone to track information of football on TV. I want to do it through a small DB however I've been struggling a lot with an error I've been trying to discover in the last two weeks but I have not been able.
I have teh following code in the class I create the DB
public class DBAdapter {
public static final String KEY_ROWID = "_id";
public static final String KEY_GDATE = "gdate";
public static final String KEY_GTIME = "gtime";
public static final String KEY_GGAME = "ggame";
public static final String KEY_GCOMPETITION = "gcompetition";
public static final String KEY_GCHANNEL = "gchannel";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "FonTV";
private static final String DATABASE_TABLE = "games";
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_CREATE =
"create table games (_id integer primary key autoincrement, "
+ "gdate text not null, gtime text not null, ggame text not null" +
"gcompetition text not null, gchannel text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
}
//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert a contact into the database---
public long insertContact(String gdate, String gtime, String ggame, String gcompetition, String gchannel )
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_GDATE, gdate);
initialValues.put(KEY_GTIME, gtime);
initialValues.put(KEY_GGAME, ggame);
initialValues.put(KEY_GCOMPETITION, gcompetition);
initialValues.put(KEY_GCHANNEL, gchannel);
return db.insert(DATABASE_TABLE, null, initialValues);
}
}
And the main class where I try to insert data has this:
public class ResultsDBActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DBAdapter db = new DBAdapter(this);
db.open();
long id = db.insertContact("12/may", "08:00", "Leeds vs York", "JUME Cup", "ITV, BBC");
id = db.insertContact("13/may", "09:00", "London vs Bath", "JUME Cup", "ITV, BBC2");
db.close();
}
}
However when I try to run it, the debugger shows and error that says:
E/Database(330): android.database.sqlite.SQLiteException: no such table: games: , while compiling: INSERT INTO games(gchannel, ggame, gtime, gdate, gcompetition) VALUES(?, ?, ?, ?, ?);
I’ve trying to change parameters and many things but I haven’t found where the problem is. Can someone help me please?
Re: Problem developing app for Android
Please use [code] tags [/code] when posting code if you want people to be able to read the code you post.
You need to create a table in the database before you can use it. I have no idea how to do that for Android (offhand), but I'm sure there's a decent description of it on the Android developers site.
Re: Problem developing app for Android
Re: Problem developing app for Android
Moved from New to Java
db
Re: Problem developing app for Android
Hi jume28,
Here is the custom class for creating Sqlite db which is more helpful..
CustomDatabase.java
Code:
package com.magesh;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class CustomDatabase {
SQLiteDatabase db = null;
Context context;
CustomDatabase(Context c) {
context = c;
}
public boolean createDatabase(String dbname) {// Checked
boolean result = false;
try {
db = context.openOrCreateDatabase(dbname, 0, null);
result = true;
} catch (Exception e) {
Log.e("Error", e.toString());
}
return result;
}
public void closeDatabase() {// Checked
db.close();
}
public boolean createTable(String TableName, String[] FieldName,
String[] FieldType) {// Checked
boolean result = false;
String query = "CREATE TABLE " + TableName + " (";
for (int i = 0; i < FieldName.length; i++) {
query = query + FieldName[i] + " " + FieldType[i] + ", ";
}
query = query.substring(0, query.lastIndexOf(","));
query = query + ");";
try {
db.execSQL(query);
result = true;
} catch (Exception e) {
Log.e("Error", e.toString());
}
return result;
}
public boolean insertRow(String TableName, String[] Values) {// Checked
boolean result = false;
String query = "INSERT INTO " + TableName + " VALUES (";
for (int i = 0; i < Values.length; i++) {
query = query + "'" + Values[i] + "', ";
}
query = query.substring(0, query.lastIndexOf(","));
query = query + ");";
try {
db.execSQL(query);
result = true;
} catch (Exception e) {
Log.e("Error", e.toString());
}
return result;
}
public String[][] showAllRecords(String TableName) {//Checked
String[][] result = null;
int rowCount, columnCount;
Cursor c=null;
String query = "SELECT * FROM " + TableName;
try {
c = db.rawQuery(query, null);
rowCount = c.getCount();
columnCount = c.getColumnCount();
result = new String[rowCount][];
for (int i = 0; i < rowCount; i++) {
result[i] = new String[columnCount];
}
int row = 0;
c.moveToFirst();
if (c != null) {
do {
for (int j = 0; j < columnCount; j++) {
result[row][j] = c.getString(j);
}
row++;
} while (c.moveToNext());
}
c.close();
} catch (Exception e) {
}
return result;
}
public boolean deleteRow(String TableName, String FieldName,
String FieldValue) {// Checked
boolean result = false;
String query = "DELETE FROM " + TableName + " WHERE " + FieldName
+ " = '" + FieldValue + "';";
try {
db.execSQL(query);
result = true;
} catch (Exception e) {
Log.e("Error", e.toString());
}
return result;
}
public boolean deleteRow(String TableName, String[] FieldName,
String[] FieldValue) {
boolean result = false;
String query = "DELETE FROM " + TableName + " WHERE ";
for (int i = 0; i < FieldName.length; i++) {
query = query + FieldName[i] + " = '"+FieldValue[i]+"'";
if(i!=FieldName.length-1)
{
query = query + " AND ";
}
}
query = query + ";";
try {
db.execSQL(query);
result = true;
} catch (Exception e) {
Log.e("Error", e.toString());
}
return result;
}
public boolean deleteAllRows(String TableName) { // Checked
boolean result = false;
String query = "DELETE FROM " + TableName + ";";
try {
db.execSQL(query);
result = true;
} catch (Exception e) {
Log.e("Error", e.toString());
}
return result;
}
}
DBTest.java
Code:
package com.magesh;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
public class DBTest extends Activity {
CustomDatabase cd;
String[][] s;
ListView lv;
TextView out;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
out = (TextView) findViewById(R.id.textView1);
String[] fn = { "Name" ,"number","sadf","asdfsd" };
String[] ft = { "Text","Text","Text","Text" };
String[] data = { "d1","d2","dd3","d4" };
cd = new CustomDatabase(this);
cd.createDatabase("Hello");
//cd.createTable("q1", fn, ft);
//cd.insertRow("q1", data);
s = cd.showAllRecords("q1");
boolean d = cd.deleteRow("q1",fn,ft);
out.setText(""+d);
cd.closeDatabase();
for (int i = 0; i < s.length; i++) {
for (int j = 0; j < s[i].length; j++) {
System.out.print("-- "+s[i][0] );
}
System.out.println();
}
}
}