Gimbal5401
Lurker
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();
}
}
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();
}
}

