I am making a quick program to get basic database functions working before i create a larger app but my application keeps force closing when i try and read from the database.
I am an amature at android programming and im just starting out. Does anyone know what is wrong? there doesnt seem to be an error in my code. 2.3.3.
DBHelper Class
package com.myprog;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBhelper extends SQLiteOpenHelper{
public final static String DB_Name = "trial_db";
public final static int DB_Version = 1;
public final static String table_name1 = "StockTakes";
public final static String stocktake_ID = "ID";
public final static String stocktake_Date = "ST_DATE";
public final static String stocktake_TakenBy = "ST_TAKEN_BY";
public final static String stocktake_Results_Table = "StockTake_Table_Name";
public DBhelper(Context context) {
super(context, DB_Name, null, DB_Version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = String.format("CREATE TABLE %s (%s TEXT, %s TEXT, %s TEXT, %s TEXT);", table_name1,
stocktake_ID,
stocktake_Date,
stocktake_TakenBy,
stocktake_Results_Table);
db.execSQL(sql);
db.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + table_name1);
this.onCreate(db);
}
}
Main class
package com.myprog;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class DatabaseTrial extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dbtrial);
final DBhelper dbHelper = new DBhelper(this);
Button dbsubmit = (Button) findViewById(R.id.submittodb1);
dbsubmit.setOnClickListener(new View.OnClickListener(){
public void onClick(View V){
ContentValues values = new ContentValues();
EditText word_one = (EditText) findViewById(R.id.idwordone);
String word_one_value = word_one.toString();
values.put(DBhelper.stocktake_ID, word_one_value);
word_one.setText("");
EditText word_two = (EditText) findViewById(R.id.idwordtwo);
String word_two_value = word_two.toString();
values.put(DBhelper.stocktake_Date, word_two_value);
word_two.setText("");
EditText word_three = (EditText) findViewById(R.id.idwordthree);
String word_three_value = word_three.toString();
values.put(DBhelper.stocktake_TakenBy, word_three_value);
word_three.setText("");
EditText word_four = (EditText) findViewById(R.id.idwordfour);
String word_four_value = word_four.toString();
values.put(DBhelper.stocktake_Results_Table, word_four_value);
word_four.setText("");
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.insert(DBhelper.table_name1, null, values);
db.close();
}
});
Button get_results = (Button) findViewById(R.id.dbgetresults);
get_results.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query(false, DBhelper.table_name1,
null, null, null, null, null, null, null);
startManagingCursor(cursor);
int collcoun = cursor.getColumnCount();
TextView display_one = (TextView) findViewById(R.id.dbresult1);
String curcount = (String) cursor.getString(1);
display_one.setText(collcoun);
cursor.close();
db.close();
}
});
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:id="@+id/idwordone"
android:layout_height="wrap_content">
</EditText>
<EditText
android:layout_width="match_parent"
android:id="@+id/idwordtwo"
android:layout_height="wrap_content">
</EditText>
<EditText
android:layout_width="match_parent"
android:id="@+id/idwordthree"
android:layout_height="wrap_content">
</EditText>
<EditText
android:layout_width="match_parent"
android:id="@+id/idwordfour"
android:layout_height="wrap_content">
</EditText>
<Button
android:text="Button"
android:id="@+id/submittodb1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</Button>
<TextView
android:text="Result Zero ;)"
android:id="@+id/dbresult1"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:textSize="24sp">
</TextView>
<TextView
android:text="Result One"
android:id="@+id/dbresult2"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:textSize="24sp">
</TextView>
<TextView
android:text="Result Two"
android:id="@+id/dbresult3"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:textSize="24sp">
</TextView>
<TextView
android:text="Result Three"
android:id="@+id/dbresult4"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:textSize="24sp">
</TextView>
<Button
android:text="Get Db Results"
android:id="@+id/dbgetresults"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
Its likely something very simple but thanks in advance. Im flat out of ideas =/.