0

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 =/.

4
Contributors
11
Replies
13
Views
6 Years
Discussion Span
Last Post by stanleyben
0

My program only crashes when i click the bottom botton to access the cursor.

0

Sorry I do not have time to check your code, but you can get working copy from my github here and compare way of connectivity. Application is working copy of application from Pro Android 3 book, chapter 3

0

In the getdbresults button listener you use the line

Cursor cursor = db.query(false, DBhelper.table_name1, 
			       		null, null, null, null, null, null, null);

Reading the android developers reference docs there is only one form of query beginning with a boolean and this uses eight further arguments beginning with the table name and a string array or the fields to be read!
You have the table name as DBhelper.table_name1, but null for the string array. This must either crash or return an empty cursor having read nothing! You need a string array with the names of the fields in it.

0

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();
				
					String columns[] = {
						
						dbHelper.stocktake_ID,
						dbHelper.stocktake_Date,
						dbHelper.stocktake_TakenBy,
						dbHelper.stocktake_Results_Table
						
				};
				
				Cursor cursor = db.query(false, dbHelper.table_name1, 
			       		columns, null, null, null, null, null, null);
				
				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 =/.

Edited by Archenemie: n/a

0

Further reading of the reference docs shows that, while the use of null for the fields to be read is discouraged it should return all columns okay. This thereore would not have been your problem (sorry)

As an observation, after reading the database you find the cursor count as an integer collcount then extract the string from column one as curcount. However you then try to insert the integer collcount as text into the textview. Should this not be curcount?

0

currcount is an ununsed variable at the moment, i was trying to set the TextView to this variable however my program kept crashing so i added the collcount variable just to see if the cursor was returning any results at all, but i could not get this to work as my program continued to crash anyway.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.