I have been trying to display records from a database in ListView. i am able to add records as confirmed using adb shell commands. i can view the records there. but when i tried to implement a view button to display records it just crashes with the error : illegalArgumentException. column_id not existing is the root of it. but i am sure it exists! please help as this is driving me absolutely mad!
DISPLAYCURSOR code:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_cursor);
filldata();
}
@SuppressWarnings("deprecation")
public void filldata(){
db.open();
Cursor cursor = db.getAllRecords();
startManagingCursor(cursor);
String[] columns = new String[] {DBAdapter.invest_type,DBAdapter.curr_per_share_price, DBAdapter.share_name,
DBAdapter.no_of_shares,DBAdapter.share_identity,DBAdapter.purchase_price,
DBAdapter.purchase_from,DBAdapter.purchase_date,DBAdapter.purchase_contact};
int[] to = new int[] { R.id.investmenttype,R.id.currpershareprice,R.id.sharename,R.id.shareno,R.id.shareid,
R.id.purprice,
R.id.purfrom,R.id.purdate,R.id.purcon};
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter( this, R.layout.row, cursor, columns, to);
this.setListAdapter(mAdapter);
db.close();
}
}
DBAdapter getallrecords
function:
public Cursor getAllRecords()
{
return db.query(DATABASETABLE, new String[] { _id,invest_type,curr_per_share_price,
share_name ,no_of_shares,share_identity,purchase_price,purchase_from,purchase_date,purchase_contact}, null,
null, null,null, null); }
ROW.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/investmenttype"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="27dp" />
<TextView
android:id="@+id/currpershareprice"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/investmenttype"
android:layout_alignBottom="@+id/investmenttype"
android:layout_toRightOf="@+id/investmenttype" />
<TextView
android:id="@+id/sharename"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/currpershareprice"
android:layout_alignBottom="@+id/currpershareprice"
android:layout_toRightOf="@+id/currpershareprice" />
<TextView
android:id="@+id/shareno"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/sharename"
android:layout_alignBottom="@+id/sharename"
android:layout_toRightOf="@+id/sharename" />
<TextView
android:id="@+id/shareid"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/shareno"
android:layout_alignBottom="@+id/shareno"
android:layout_toRightOf="@+id/shareno" />
<TextView
android:id="@+id/purprice"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/shareid"
android:layout_alignBottom="@+id/shareid"
android:layout_toRightOf="@+id/shareid" />
<TextView
android:id="@+id/purfrom"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/purprice"
android:layout_alignBottom="@+id/purprice"
android:layout_toRightOf="@+id/purprice" />
<TextView
android:id="@+id/purdate"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/purfrom"
android:layout_alignBottom="@+id/purfrom"
android:layout_toRightOf="@+id/purfrom" />
<TextView
android:id="@+id/purcon"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/purdate"
android:layout_alignBottom="@+id/purdate"
android:layout_toRightOf="@+id/purdate" />
</LinearLayout>
</ScrollView>
activity_display_cursor
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nodata"/>
</LinearLayout>
entire DBAdapter code:
package com.example.studentdatabase;
import android.content.ContentValues;
public class DBAdapter{
public static final String DATABASENAME = "new";
public static final String DATABASETABLE="equities";
public static final String _id="id";
public static final String invest_type = "investmenttype";
public static final String curr_per_share_price = "currentshareprice";
public static final String share_name="sharename";
public static final String no_of_shares="noofshares";
public static final String share_identity="shareidentity";
public static final String purchase_price="purchaseprice";
public static final String purchase_from="webagent";
public static final String purchase_date="purchasedate";
public static final String purchase_contact="agentcontactno";
public static final String DATABASECREATE=
"create table if not exists equities(" +
"id INTEGER PRIMARY KEY," +
"investmenttype VARCHAR not null," +
"currentshareprice INTEGER," +
"sharename VARCHAR not null," +
"noofshares INTEGER,"+
"shareidentity VARCHAR not null,"+
"purchaseprice INTEGER," +
"webagent VARCHAR not null," +
"purchasedate DATE,"+
"agentcontactno INTEGER);";
public static final int DATABASEVERSION= 38;
public static final String TAG="DBAdapter";
private final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
public class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context,DATABASENAME,null,DATABASEVERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
// try {
db.execSQL(DATABASECREATE);
//
}
/* catch (SQLException e) {
e.printStackTrace();
}
*/
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS equities");
onCreate(db);
}
}
//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
public long addRecord(String investmenttype,String currentshareprice,String noofshares,String
sharename,String shareidentity,String purchaseprice,
String purchasedate,String webagent,String agentcontactno)
{
ContentValues initialValues = new ContentValues();
initialValues.put(invest_type, investmenttype);
initialValues.put(curr_per_share_price,currentshareprice);
initialValues.put(share_name, sharename);
initialValues.put(no_of_shares, noofshares);
initialValues.put(share_identity,shareidentity);
initialValues.put(purchase_price, purchaseprice);
initialValues.put(purchase_from, webagent);
initialValues.put(purchase_date, purchasedate);
initialValues.put(purchase_contact,agentcontactno);
return db.insert(DATABASETABLE, null, initialValues);
}
//---retrieves a particular record---
public Cursor getRecord(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASETABLE, new String[] {
_id,
invest_type, curr_per_share_price, share_name,no_of_shares,share_identity,purchase_price,
purchase_from,purchase_date,purchase_contact
}
,
_id + "=" + rowId, null, null, null,null,null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor getAllRecords()
{
return db.query(DATABASETABLE, new String[] {
"_id","invest_type","curr_per_share_price",
"share_name","no_of_shares","share_identity","purchase_price","purchase_from","purchase_date","purchase_contact"
}
,
null, null, null, null, null);
}
}
Logcat
06-10 12:38:17.965: I/SqliteDatabaseCpp(649): sqlite returned: error code = 1, msg = no such column: _id,
db=/data/data/com.example.studentdatabase/databases/new 06-10 12:38:17.965: D/AndroidRuntime(649): Shutting down VM 06-10 12:38:17.965: W/dalvikvm(649): threadid=1: thread exiting with uncaught exception (group=0x409961f8) 06-10 12:38:18.045: E/AndroidRuntime(649): FATAL EXCEPTION: main 06-10 12:38:18.045: E/AndroidRuntime(649): java.lang.RuntimeException: Unable to start activity C ComponentInfo{com.example.studentdatabase/com.example.studentdatabase.DisplayCursor}: android.database.sqlite.SQLiteException: no such column: _id: , while compiling: SELECT _id, invest_type, curr_per_share_price, share_name, no_of_shares, share_identity, purchase_price, purchase_from, purchase_date, purchase_contact FROM equities 06-10 12:38:18.045: E/AndroidRuntime(649): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955) 06-10 12:38:18.045: E/AndroidRuntime(649): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980) 06-10 12:38:18.045: E/AndroidRuntime(649): at android.app.ActivityThread.access$600(ActivityThread.java:122) 06-10 12:38:18.045: E/AndroidRuntime(649): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146) 06-10 12:38:18.045: E/AndroidRuntime(649): at android.os.Handler.dispatchMessage(Handler.java:99) 06-10 12:38:18.045: E/AndroidRuntime(649): at android.os.Looper.loop(Looper.java:137) 06-10 12:38:18.045: E/AndroidRuntime(649): at android.app.ActivityThread.main(ActivityThread.java:4340) 06-10 12:38:18.045: E/AndroidRuntime(649): at java.lang.reflect.Method.invokeNative(Native Method) 06-10 12:38:18.045: E/AndroidRuntime(649): at java.lang.reflect.Method.invoke(Method.java:511) 06-10 12:38:18.045: E/AndroidRuntime(649): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 06-10 12:38:18.045: E/AndroidRuntime(649): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 06-10 12:38:18.045: E/AndroidRuntime(649): at dalvik.system.NativeStart.main(Native Method) 06-10 12:38:18.045: E/AndroidRuntime(649): Caused by: android.database.sqlite.SQLiteException: no such c column: _id: , while compiling: SELECT _id, invest_type, curr_per_share_price, share_name, no_of_shares, share_identity, purchase_price, purchase_from, purchase_date, purchase_contact FROM equities 06-10 12:38:18.045: E/AndroidRuntime(649): at
android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 06-10 12:38:18.045: E/AndroidRuntime(649): at android.database.sqlite.SQLiteCompiledSql. (SQLiteCompiledSql.java:64) 06-10 12:38:18.045: E/AndroidRuntime(649): at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143) 06-10 12:38:18.045: E/AndroidRuntime(649): at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361) 06-10 12:38:18.045: E/AndroidRuntime(649): at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:127)
06-10 12:38:18.045: E/AndroidRuntime(649): at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:94)
06-10 12:38:18.045: E/AndroidRuntime(649): at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:53)
06-10 12:38:18.045: E/AndroidRuntime(649): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47) 06-10 12:38:18.045: E/AndroidRuntime(649): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564) 06-10 12:38:18.045: E/AndroidRuntime(649): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1449) 06-10 12:38:18.045: E/AndroidRuntime(649): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405) 06-10 12:38:18.045: E/AndroidRuntime(649): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1485) 06-10 12:38:18.045: E/AndroidRuntime(649): at com.example.studentdatabase.DBAdapter.getAllRecords(DBAdapter.java:127) 06-10 12:38:18.045: E/AndroidRuntime(649): at com.example.studentdatabase.DisplayCursor.filldata(DisplayCursor.java:34) 06-10 12:38:18.045: E/AndroidRuntime(649): at com.example.studentdatabase.DisplayCursor.onCreate(DisplayCursor.java:27) 06-10 12:38:18.045: E/AndroidRuntime(649): at android.app.Activity.performCreate(Activity.java:4465) 06-10 12:38:18.045: E/AndroidRuntime(649): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 06-10 12:38:18.045: E/AndroidRuntime(649): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919) 06-10 12:38:18.045: E/AndroidRuntime(649): ... 11 more