Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development

Application Development Dev Lounge for the Coder Folks



Reply
 
LinkBack Thread Tools
Old August 5th, 2011, 05:46 AM   #1 (permalink)
New Member
 
Join Date: Jul 2010
Posts: 3
 
Device(s):
Thanks: 1
Thanked 0 Times in 0 Posts
Default SQLite issues. Please help!!

I'm new to this android programming lark (although not to programming) and am trying to build a simple application that needs to store some information to a SQLite database.
I have implemented a DatabaseHelper.java class and created an instance of it in my activity, but the code falls into the catch block when i try to do an insert.

I have a feeling that its something simple like I haven't actually created the database properly, but I cant spot where the problem lies.

Could someone help me please?
Even a nod in the right direction would be greatly appreciated.

The DatabaseHelper.java and the Avtivity I am calling it from are shown below.

Thanks.

DatabaseHelper.java

package com.kingshaybulling.www;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DatabaseHelper extends SQLiteOpenHelper {

static final String dbName="bullingDB";
static final String cowTable="Cow";
static final String colID="_id";
static final String colNumber="CowNumber";
static final String colDate="CowDate";
static final String colObserved="CowObserved";

static final String viewCows="ViewCows";

public DatabaseHelper(Context context) {
super(context, dbName, null,33);

// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub

AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(null).create();
alertDialog.setTitle("HERE");
alertDialog.setMessage("HERE!!!");
alertDialog.show();

try
{
db.execSQL("CREATE TABLE "+cowTable+" ("+colID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
colNumber+" TEXT, "+colDate+" Integer, "+colObserved+" TEXT);");


db.execSQL("CREATE VIEW "+viewCows+
" AS SELECT "+cowTable+"."+colID+" AS _id,"+
" "+cowTable+"."+colNumber+","+
" "+cowTable+"."+colDate+","+
" "+cowTable+"."+colObserved+""+
" FROM "+cowTable );

}
catch(Exception Ex)
{

}
finally
{

}
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS "+cowTable);
db.execSQL("DROP VIEW IF EXISTS "+viewCows);
onCreate(db);
}

void AddCow(Cow cow)
{
SQLiteDatabase db= this.getWritableDatabase();

ContentValues cv=new ContentValues();

cv.put(colNumber, cow.getNumber());
cv.put(colDate, cow.getCowDate());
cv.put(colObserved, cow.getObserved());
//cv.put(colDept,2);

db.insert(cowTable, colNumber, cv);
db.close();
}

int getCowCount()
{
SQLiteDatabase db=this.getWritableDatabase();
Cursor cur= db.rawQuery("Select * from "+cowTable, null);
int x= cur.getCount();
cur.close();
return x;
}

Cursor getAllCows()
{
SQLiteDatabase db=this.getWritableDatabase();

//Cursor cur= db.rawQuery("Select "+colID+" as _id , "+colName+", "+colAge+" from "+employeeTable, new String [] {});
Cursor cur= db.rawQuery("SELECT * FROM "+viewCows,null);
return cur;
}
}





Activity.java

package com.kingshaybulling.www;

import java.math.BigDecimal;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

public class AddCowActivity extends Activity {
DatabaseHelper dbHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addcow);

//Handle button
Button SaveButton = (Button) findViewById(R.id.button1);
SaveButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
HandleSaveButtonClick();
}
});
}

private void HandleSaveButtonClick() {

//set up text boxes
EditText cowText = (EditText) findViewById(R.id.editText1);
//set up the radio buttons
RadioButton rb0 = (RadioButton) findViewById(R.id.radio0);
RadioButton rb1 = (RadioButton) findViewById(R.id.radio1);

//set up getting values
String cowString = cowText.getText().toString();
String Observed = "";
String Served = "";
if (rb0.isChecked()==true)
{
Observed = "True";
Served = "False";
}
if (rb1.isChecked()==true)
{
Observed = "False";
Served = "True";
}

//set the text view to see if working
TextView result1 = (TextView) findViewById(R.id.textView2);
TextView result2 = (TextView) findViewById(R.id.textView3);
TextView result3 = (TextView) findViewById(R.id.textView4);

result1.setText(cowString);
result2.setText(Observed);
result3.setText(Served);

String currentDateString = DateFormat.getDateInstance().format(new Date());

try
{
Cow cow=new Cow(cowString, currentDateString, Observed);
result2.setText("done cow");
dbHelper.AddCow(cow);
result2.setText("added cow");
}
catch(Exception ex)
{
//ok=false;
//CatchError(ex.toString());
result3.setText("Error");
}
finally
{
//if(ok)
//{
//NotifyEmpAdded();
//Alerts.ShowEmpAddedAlert(this);
//txtEmps.setText("Number of employees "+String.valueOf(dbHelper.getEmployeeCount())) ;
//}
}
}

public static double round(double unrounded, int precision, int roundingMode){
BigDecimal bd = new BigDecimal(unrounded);
BigDecimal rounded = bd.setScale(precision, roundingMode);
return rounded.doubleValue();
}
}

Gimbal5401 is offline  
Reply With Quote
Sponsors
Old August 5th, 2011, 11:26 AM   #2 (permalink)
Junior Member
 
Join Date: Dec 2010
Location: Colorado (USA)
Posts: 60
 
Device(s):
Thanks: 0
Thanked 10 Times in 10 Posts
Default

I think your issue is with getting the database. I could never get getWritableDatabase to work.

So instead of this:
SQLiteDatabase db= this.getWritableDatabase();

You may try:
SQLiteDatabase db= SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

You'll need to define myPath as:
String myPath = dbPath + dbName;

I see you have dbName defined but not the actual path.

dbPath would be like this if you are storing the data on the phone and not on the SIM:
static final String dbPath="/data/data/com.kingshaybulling.www/databases/";
mills2533 is offline  
Last edited by mills2533; August 5th, 2011 at 11:28 AM.
Reply With Quote
The Following User Says Thank You to mills2533 For This Useful Post:
Gimbal5401 (August 8th, 2011)
Old August 8th, 2011, 03:18 AM   #3 (permalink)
New Member
 
Join Date: Jul 2010
Posts: 3
 
Device(s):
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks for getting back to me on this.
I've made the changes that you suggest and am still getting an error.
Its all good until i call dbHelper.AddCow(cow);
and i get a null pointer exception.

I'm not sufficiently used to eclipse and java to be able to trace the problem and work out exactly what the issue is, so I'm really stuck.
Do you have any ideas on what I can try next?

thanks

EDIT:

I've spotted that i was missing a call (dbHelper = new DatabaseHelper(this)

I now have a different error:

SQLiteCantOpenDatabaseException:unable to open database file

so would that point to something i've done wrong in the helper class where I've created the database?
Gimbal5401 is offline  
Last edited by Gimbal5401; August 8th, 2011 at 10:22 AM. Reason: further info
Reply With Quote
Old August 9th, 2011, 10:16 AM   #4 (permalink)
Junior Member
 
Join Date: Dec 2010
Location: Colorado (USA)
Posts: 60
 
Device(s):
Thanks: 0
Thanked 10 Times in 10 Posts
Default

That is possible. I'd check to make sure the database file exists under the path and filename you are using. You can do that using "adb shell" if you're familiar with that. You can also open the database file using sqlite while in the adb to be certain it can be opened.
mills2533 is offline  
Reply With Quote
Old August 9th, 2011, 10:48 AM   #5 (permalink)
New Member
 
Join Date: Jul 2010
Posts: 3
 
Device(s):
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I dont really understand the adb shell thing.
Will have to look into it.
cant believe that this problem has taken up so much of my time.
never mind. at least when i finally figure it out I'll never have the same issue again. lol
Gimbal5401 is offline  
Reply With Quote
Old August 9th, 2011, 11:23 AM   #6 (permalink)
Junior Member
 
Join Date: Dec 2010
Location: Colorado (USA)
Posts: 60
 
Device(s):
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Debugging is great for learning! All the things I'm recommending came from going through the same thing you are right now.

You may want to checkout these database tutorials:
Using your own SQLite database in Android applications | ReignDesign Blog
Another Android Blog Android Database Tutorial

I learned a lot from them. The first link deals a lot with creating and opening a database. The second is a lot of reading and code but worthwhile.
mills2533 is offline  
Reply With Quote
Reply

Bookmarks

Tags
android, database, programming, sqlite


Go Back   Android Forums > Android Development > Application Development User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 11:43 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo