• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Table not being created in SQLite

gotenks05

Lurker
It's been a while since I was here. Anyway, I recently made an update to the iOS version of my app, which now allows users to save and load data via SQLite. However, I do not want to leave Android users out in the cold, so I decided to try and bring an SQLite back-end to the Android version too. However, I am having some issues. When I launch my app in the emulator, my app crashes. Upon inspection via adb shell, I noticed that the table is not being created, since no tables are listed in the database. How do I fix this?

Here is the code for my handler class:

Code:
import java.util.*;
import android.content.*;
import android.database.*;
import android.database.sqlite.*;
import android.database.sqlite.*;

public class DBHandler extends SQLiteOpenHelper
{
	// static variables
	private static final int DATABASE_VERSION = 1; // version
	private static final String DATABASE_NAME = "mrd.db"; /* database name */
	private static final String TABLE_NAME = "rmd"; // db name

	// field names
	private static final String key = "id";
	private static final String bd = "birth";
	private static final String ira = "bal";
	private static final String distrib = "year";
    
    // variables to hold data
    String birth;
    double bal;
    int year, records;

	public DBHandler(Context context)
	{
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	// method to create table
	@Override
	public void onCreate(SQLiteDatabase db)
	{
		String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + key + " INTEGER PRIMARY KEY AUTOINCREMENT, " + bd + " TEXT, " + ira + " REAL, " + distrib + " INTEGER)";

		db.execSQL(CREATE_TABLE);
	}

	// method to upgrade database
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		// drop table if exists
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

		// recreate table
		onCreate(db);
	}

	// create insert method
	public void saveData(String a, double b, int c)
	{
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(bd, a);
		values.put(ira, b);
		values.put(distrib, c);

		// insert statement
		db.insert(TABLE_NAME, null, values);
        db.close();
	}

	// create update method
	public int updateData(String a, double b, int c)
	{
        int test;
		SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        
		if (getBirth().equals(a) != true && getBal() != b && getYear() != c)
		{
            values.put(bd, a);
            values.put(ira, b);
            values.put(distrib, c);
			if (db.update(TABLE_NAME, values, key + " = ?", new String[] { Integer.toString(1)}) != 0)
            {
                test = 1;
            }
            else
            {
                test = 0;
            }
		}
		else if (getBirth().equals(a) != true && getBal() != b)
		{
            values.put(bd, a);
            values.put(ira, b);
			if (db.update(TABLE_NAME, values, key + " = ?", new String[] { Integer.toString(1)}) != 0)
            {
                test = 1;
            }
            else
            {
                test = 0;
            }
		}
		else if (getBirth().equals(a) != true && getYear() != c)
		{
            values.put(bd, a);
            values.put(distrib, c);
			if (db.update(TABLE_NAME, values, key + " = ?", new String[] { Integer.toString(1)}) != 0)
            {
                test = 1;
            }
            else
            {
                test = 0;
            }
		}
		else if (getBal() != b && getYear() != c)
		{
            values.put(ira, b);
            values.put(distrib, c);
			if (db.update(TABLE_NAME, values, key + " = ?", new String[] { Integer.toString(1)}) != 0)
            {
                test = 1;
            }
            else
            {
                test = 0;
            }
		}
		else if (getYear() != c)
		{
            values.put(distrib, c);
			if (db.update(TABLE_NAME, values, key + " = ?", new String[] { Integer.toString(1)}) != 0)
            {
                test = 1;
            }
            else
            {
                test = 0;
            }
		}
		else if (getBal() != b)
		{
            values.put(ira, b);
			if (db.update(TABLE_NAME, values, key + " = ?", new String[] { Integer.toString(1)}) != 0)
            {
                test = 1;
            }
            else
            {
                test = 0;
            }
		}
		else if (getBirth().equals(a) != true)
		{
            values.put(bd, a);
			if (db.update(TABLE_NAME, values, key + " = ?", new String[] { Integer.toString(1)}) != 0)
            {
                test = 1;
            }
            else
            {
                test = 0;
            }
		}
		else
        {
            test = 0;
        }
		return test;
	}

	// the following methods retrieve data
	public String getBirth()
	{
        String birthQuery = "SELECT date(" + bd + ") FROM rmd LIMIT 1";
		SQLiteDatabase db = this.getReadableDatabase();

		Cursor cursor = db.rawQuery(birthQuery, null);
        if (cursor != null)
            cursor.moveToFirst();
        birth = cursor.getString(0);
        return birth;
        
	}

	public double getBal() throws SQLException
	{
        String balQuery = "SELECT " + ira + " FROM rmd LIMIT 1";
		SQLiteDatabase db = this.getReadableDatabase();
        
		Cursor cursor = db.rawQuery(balQuery, null);
        if (cursor != null)
            cursor.moveToFirst();
        bal = cursor.getDouble(0);
        return bal;
	}

	public int getYear() throws SQLException
	{
        String distQuery = "SELECT " + distrib + " FROM rmd LIMIT 1";
		SQLiteDatabase db = this.getReadableDatabase();
        
		Cursor cursor = db.rawQuery(distQuery, null);
        if (cursor != null)
            cursor.moveToFirst();
            year = cursor.getInt(0);
            return year;
	}

	// the following method gets number of records
	public int records() throws SQLException
	{
        String rowCount = "SELECT * FROM " + TABLE_NAME;
		SQLiteDatabase db = this.getReadableDatabase();
        
		Cursor cursor = db.rawQuery(rowCount, null);
        cursor.close();
        return records = cursor.getCount();
	}
}

I am still very new to using database back-ends outside of PHP+MySQL, even though iOS was my first experience with SQLite, which was using the FMDB wrapper.

Update: Never mind, it looks like it was created, after I decided to put in the semi-colon, as if I were using SQLite via command line.
 
Back
Top Bottom