我试图运行我的第一个数据库连接程序,这是我面临的问题。
Couldn't read row 0, col -1 from CursorWindow.
这是我在调用showData()函数时得到的错误,我猜这意味着它试图从第0行和第-1列获取第一个元素值。
为什么它从-1开始,以及如何从column = 1开始 package com.example.garegin.data;
public class MainActivity extends Activity {
SQLiteDatabase db;
String fName, sName, mail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db=openOrCreateDatabase("MyDB1",MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS Student(fName VARCHAR, sName VARCHAR, mail VARCHAR);");
Button save = (Button) findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addData(view);
}
});
Button list = (Button) findViewById(R.id.list);
list.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showData(view);
}
});
}
private void addData(View view){
EditText text1 = (EditText) findViewById(R.id.fname);
EditText text2 = (EditText) findViewById(R.id.sname);
EditText text3 = (EditText) findViewById(R.id.mail);
fName=text1.getText().toString();
sName=text2.getText().toString();
mail=text3.getText().toString();
db.execSQL("INSERT INTO Student VALUES('"+fName+"', '"+sName+"', '"+mail+"')");
}
private void showData(View view){
Cursor c = db.rawQuery("SELECT * from Student", null);
int count = c.getCount();
c.moveToFirst();
TableLayout tableLayout = new TableLayout(getApplicationContext());
tableLayout.setVerticalScrollBarEnabled(true);
TableRow tableRow;
TextView textView,textView1,textView2,textView3,textView4,textView5;tableRow = new TableRow(getApplicationContext());
textView=new TextView(getApplicationContext());
textView.setText("Firstname");
textView.setTextColor(Color.RED);
textView.setTypeface(null, Typeface.BOLD);
textView.setPadding(20, 20, 20, 20);
tableRow.addView(textView);
textView4=new TextView(getApplicationContext());
textView4.setText("LastName");
textView4.setTextColor(Color.RED);
textView4.setTypeface(null, Typeface.BOLD);
textView4.setPadding(20, 20, 20, 20);
tableRow.addView(textView4);
textView5=new TextView(getApplicationContext());
textView5.setText("Email");
textView5.setTextColor(Color.RED);
textView5.setTypeface(null, Typeface.BOLD);
textView5.setPadding(20, 20, 20, 20);
tableRow.addView(textView5);
tableLayout.addView(tableRow);
for (Integer j = 0; j < count; j++)
{
tableRow = new TableRow(getApplicationContext());
textView1 = new TextView(getApplicationContext());
textView1.setText(c.getString(c.getColumnIndex("fName")));
textView2 = new TextView(getApplicationContext());
textView2.setText(c.getString(c.getColumnIndex("sName")));
textView3 = new TextView(getApplicationContext());
textView3.setText(c.getString(c.getColumnIndex("mail")));
textView1.setPadding(20, 20, 20, 20);
textView2.setPadding(20, 20, 20, 20);
textView3.setPadding(20, 20, 20, 20);
tableRow.addView(textView1);
tableRow.addView(textView2);
tableRow.addView(textView3);
tableLayout.addView(tableRow);
c.moveToNext() ;
}
setContentView(tableLayout);
db.close();
}
public void close(View view){
System.exit(0);
}
}
答案 0 :(得分:3)
当您致电getColumnIndex()
时,您将获得Cursor
中不存在的列名的索引-1。尝试在第一行列索引-1上检索值会产生错误。
确保您的查询中包含您引用的列名称。
在添加代码后编辑:好像你有一个旧版本的数据库文件,没有全部。如果已存在具有相同名称的表,CREATE TABLE IF NOT EXISTS
将无法创建表。旧表格没有您尝试访问的列。要解决此问题,请删除旧数据库:卸载您的应用,或使用应用管理器清除其数据。