我正在从数据库中重新获取的值填充列表视图,我正在使用cursor adapter
来完成此任务,但我的问题是我收到Illegal argument exception no found column '_ID'
但我的表格有_ID
列我对这个问题很困惑,因为我无法完全理解。下面是我的代码。
表类
public class StockTable {
Context c1;
DatabaseObject d1;
Cursor c;
StockTable(Context mcontext){
c1=mcontext;
//---------------objects---------------------------//
d1=new DatabaseObject(c1);
//---------------objects ends----------------------//
}
// d1=new DatabaseObject(c1);
//-----------General Decelerations-------------------//
private String selectID;
private int storeID=0;
//-----------General Decelerations ends--------------//
//-----------table name and columns-----------------//
final String tablename="StockTable";
public String column1="Stock_ID";
public String column2="StockName";
//-----------table name and columns end-----------------//
final String stocktable = "CREATE TABLE " + tablename +
" (" + column1+ " INTEGER PRIMARY KEY , " + column2 + " TEXT ) ";
public ContentValues insert(String one){
try{
selectID="Select Max("+ column1 + ") from " + tablename;
System.out.println(selectID);
c=d1.d.db.rawQuery(selectID,null);
System.out.println(c.getCount());
//System.out.println(c.getInt(0));
if(c.moveToNext())
{
System.out.println("Has Values");
}
else
{
System.out.println("No Values");
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
ContentValues cvi=new ContentValues();
// for(int i=0;i<=1;i++)
cvi.put(column1, c.getInt(0)+1);
cvi.put(column2,one);
return cvi;
}
public void delete(){
}
public void select(){
}
}
ListView类
public class stockmanager extends Activity{
String getentry;
private int storeID=0;
DatabaseObject d;
StockTable st;
private String getstocks;
private Cursor a1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stockmanager);
d=new DatabaseObject(getApplicationContext());
st=new StockTable(getApplicationContext());
final Button AddStock=(Button) findViewById(R.id.button1);
final EditText entry=(EditText) findViewById(R.id.editText1);
final Button BroDetail=(Button) findViewById(R.id.button2);
final ListView popstocks=(ListView) findViewById(R.id.listView1);
AddStock.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getentry=entry.getText().toString();
//st.insert(getentry);
System.out.println(getentry);
//d.db.rawQuery(st.select(), null);
d.d.db.insert(st.tablename, null,st.insert(getentry));
//populatelist populatestocks=new populatelist();
getstocks="Select "+ st.column1 + " as _ID, " + st.column2 + " From "+ st.tablename;
System.out.println(getstocks);
a1=d.d.db.rawQuery(getstocks, null);
poplist populatestocks=new poplist(getApplicationContext(),a1) ;
popstocks.setAdapter(populatestocks);
}
});
}
public class poplist extends CursorAdapter{
public poplist(Context context, Cursor a1) {
super(context, a1);
// TODO Auto-generated constructor stub
}
//StockTable st1=new StockTable(getApplicationContext());
//Database d1=new Database(getApplicationContext());
@Override
public void bindView(View view, Context context, Cursor a1) {
// TODO Auto-generated method stub
final CheckBox cb=(CheckBox) view.findViewById(R.id.checkBox1);
final Button view1=(Button) view.findViewById(R.id.button1);
if(a1.moveToFirst()){
//cb.setText(a1.getString(a1.getColumnIndex(st1.column2)));
do{
cb.setText(a1.getString(a1.getColumnIndexOrThrow(st.column2)));
}while (a1.moveToNext());
}
}
@Override
public View newView(Context context, Cursor a1, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.stocklist, parent, false);
bindView(v, context, a1);
return v;
// return null;
}
}
public void Declerations(){
}
}
logcat的
02-11 16:30:16.903: D/libEGL(4614): loaded /system/lib/egl/libEGL_genymotion.so
02-11 16:30:16.919: D/(4614): HostConnection::get() New Host Connection established 0xb94fd3d0, tid 4614
02-11 16:30:17.011: D/libEGL(4614): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
02-11 16:30:17.015: D/libEGL(4614): loaded /system/lib/egl/libGLESv2_genymotion.so
02-11 16:30:17.395: W/EGL_genymotion(4614): eglSurfaceAttrib not implemented
02-11 16:30:17.411: E/OpenGLRenderer(4614): Getting MAX_TEXTURE_SIZE from GradienCache
02-11 16:30:17.499: E/OpenGLRenderer(4614): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
02-11 16:30:17.503: D/OpenGLRenderer(4614): Enabling debug mode 0
02-11 16:32:10.399: D/libEGL(4800): loaded /system/lib/egl/libEGL_genymotion.so
02-11 16:32:10.495: D/(4800): HostConnection::get() New Host Connection established 0xb941ae00, tid 4800
02-11 16:32:10.543: D/libEGL(4800): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
02-11 16:32:10.571: D/libEGL(4800): loaded /system/lib/egl/libGLESv2_genymotion.so
02-11 16:32:11.039: W/EGL_genymotion(4800): eglSurfaceAttrib not implemented
02-11 16:32:11.071: E/OpenGLRenderer(4800): Getting MAX_TEXTURE_SIZE from GradienCache
02-11 16:32:11.163: E/OpenGLRenderer(4800): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
02-11 16:32:11.179: D/OpenGLRenderer(4800): Enabling debug mode 0
02-11 16:32:13.691: I/Choreographer(4800): Skipped 31 frames! The application may be doing too much work on its main thread.
02-11 16:32:17.283: E/SQLiteLog(4800): (5) database is locked
02-11 16:32:17.551: E/SQLiteDatabase(4800): Failed to open database '/data/data/com.example.portfoliomanager/databases/PortfolioManager'.
02-11 16:32:17.551: E/SQLiteDatabase(4800): android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:634)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:320)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:294)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:215)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:863)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:235)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at com.example.portfoliomanager.Database.<init>(Database.java:20)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at com.example.portfoliomanager.DatabaseObject.<init>(DatabaseObject.java:12)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at com.example.portfoliomanager.StockTable.<init>(StockTable.java:16)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at com.example.portfoliomanager.Database.onCreate(Database.java:32)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at com.example.portfoliomanager.Database.<init>(Database.java:20)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at com.example.portfoliomanager.DatabaseObject.<init>(DatabaseObject.java:12)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at com.example.portfoliomanager.stockmanager.onCreate(stockmanager.java:33)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.app.Activity.performCreate(Activity.java:5133)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.os.Looper.loop(Looper.java:137)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at android.app.ActivityThread.main(ActivityThread.java:5103)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at java.lang.reflect.Method.invoke(Method.java:525)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-11 16:32:17.551: E/SQLiteDatabase(4800): at dalvik.system.NativeStart.main(Native Method)
02-11 16:32:17.555: I/System.out(4800): database is locked (code 5): , while compiling: PRAGMA journal_mode
02-11 16:32:17.903: D/dalvikvm(4800): GC_FOR_ALLOC freed 125K, 1% free 16925K/17072K, paused 36ms, total 38ms
02-11 16:32:18.215: W/EGL_genymotion(4800): eglSurfaceAttrib not implemented
02-11 16:32:22.199: I/System.out(4800): sdf
02-11 16:32:22.223: I/System.out(4800): Select Max(_ID) from StockTable
02-11 16:32:22.279: I/System.out(4800): 1
02-11 16:32:22.299: I/System.out(4800): Has Values
02-11 16:32:22.383: I/System.out(4800): Select _ID as _ID, StockName From StockTable
02-11 16:32:22.395: D/AndroidRuntime(4800): Shutting down VM
02-11 16:32:22.399: W/dalvikvm(4800): threadid=1: thread exiting with uncaught exception (group=0xa4b99648)
02-11 16:32:22.419: E/AndroidRuntime(4800): FATAL EXCEPTION: main
02-11 16:32:22.419: E/AndroidRuntime(4800): java.lang.IllegalArgumentException: column '_id' does not exist
02-11 16:32:22.419: E/AndroidRuntime(4800): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
02-11 16:32:22.419: E/AndroidRuntime(4800): at android.widget.CursorAdapter.init(CursorAdapter.java:168)
02-11 16:32:22.419: E/AndroidRuntime(4800): at android.widget.CursorAdapter.<init>(CursorAdapter.java:116)
02-11 16:32:22.419: E/AndroidRuntime(4800): at com.example.portfoliomanager.stockmanager$poplist.<init>(stockmanager.java:117)
02-11 16:32:22.419: E/AndroidRuntime(4800): at com.example.portfoliomanager.stockmanager$1.onClick(stockmanager.java:55)
02-11 16:32:22.419: E/AndroidRuntime(4800): at android.view.View.performClick(View.java:4240)
02-11 16:32:22.419: E/AndroidRuntime(4800): at android.view.View$PerformClick.run(View.java:17721)
02-11 16:32:22.419: E/AndroidRuntime(4800): at android.os.Handler.handleCallback(Handler.java:730)
02-11 16:32:22.419: E/AndroidRuntime(4800): at android.os.Handler.dispatchMessage(Handler.java:92)
02-11 16:32:22.419: E/AndroidRuntime(4800): at android.os.Looper.loop(Looper.java:137)
02-11 16:32:22.419: E/AndroidRuntime(4800): at android.app.ActivityThread.main(ActivityThread.java:5103)
02-11 16:32:22.419: E/AndroidRuntime(4800): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 16:32:22.419: E/AndroidRuntime(4800): at java.lang.reflect.Method.invoke(Method.java:525)
02-11 16:32:22.419: E/AndroidRuntime(4800): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-11 16:32:22.419: E/AndroidRuntime(4800): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-11 16:32:22.419: E/AndroidRuntime(4800): at dalvik.system.NativeStart.main(Native Method)
02-11 16:32:25.587: I/Process(4800): Sending signal. PID: 4800 SIG: 9
02-11 16:32:27.335: D/libEGL(4836): loaded /system/lib/egl/libEGL_genymotion.so
02-11 16:32:27.355: D/(4836): HostConnection::get() New Host Connection established 0xb937bb00, tid 4836
02-11 16:32:27.387: D/libEGL(4836): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
02-11 16:32:27.387: D/libEGL(4836): loaded /system/lib/egl/libGLESv2_genymotion.so
02-11 16:32:27.575: W/EGL_genymotion(4836): eglSurfaceAttrib not implemented
02-11 16:32:27.587: E/OpenGLRenderer(4836): Getting MAX_TEXTURE_SIZE from GradienCache
02-11 16:32:27.635: E/OpenGLRenderer(4836): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
02-11 16:32:27.643: D/OpenGLRenderer(4836): Enabling debug mode 0
请帮我解决这个问题。
答案 0 :(得分:1)
从您的日志文件中我可以看到您的数据库已被锁定且无法打开。
也许在查询之前尝试解决这个问题!
如果您在模拟器上,可能无法访问该文件,您无权打开或者它已被损坏?
答案 1 :(得分:1)
在第一次尝试中,您是否使用SimpleCursorAdapter而不是继承CursorAdapter来完成工作?也许它可以突出一些不起眼的部分:)
第二次,您可以在迭代光标
之前增强测试if(cursor.getCount()!=0){
if(cursor.moveToFirst()){
do{
//your code
}while(cursor.moveToNext());
}
}
答案 2 :(得分:0)
_ID是否区分大小写? Android中的常量是_ID,但实际值是“_id”。尝试使用常量BaseColumns._ID而不是“_ID”
http://developer.android.com/reference/android/provider/BaseColumns.html#_ID
答案 3 :(得分:0)
如果您正在使用预制数据库(即通过终端手动填写),请按照本教程进行操作:
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
它基本上告诉你如何让你的数据库与Android链接。然后,一旦完成,您就可以开始进行查询,即
Cursor cursor = database.query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit);