我的应用因桌面未找到错误而崩溃,原因是什么?
MainActivity
helper=new g_dbHelper((this));
SQLiteDatabase sql=helper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("NAME","Harry Potter");
values.put("PUBLISHER","Bloomberg Publications");
long newRowId = sql.insert("first",null,values);
String[] name={"NAME"};
SQLiteDatabase sql1=helper.getReadableDatabase();
try{
Cursor cursor=sql1.query("first",name,null,null,null,null,null);
int id=cursor.getColumnIndex("NAME");
TextView txt=findViewById(R.id.textViewa);
txt.setText(cursor.getString(id));
cursor.close();
} catch(SQLiteException e) {
Log.e("TAG","Problem in Reading");
}
dbHelper
public class g_dbHelper extends SQLiteOpenHelper implements BaseColumns {
final static String DB_NAME="books.db";
public g_dbHelper(Context context) {
super(context,DB_NAME, null, 2);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String create= "CREATE TABLE first ("+ BaseColumns._ID+" INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, PUBLISHER TEXT);";
try {
sqLiteDatabase.execSQL(create);
} catch (SQLiteException e) {
Log.e("TAG","Table not created");
}
}
}
我还尝试更改数据库版本以及卸载并重新安装应用程序。
答案 0 :(得分:0)
创建表查询格式错误。并且在创建表时也没有使用try catch,因为必须这样做。以下是解释。
public static final String COL_NAME="_name";
public static final String COL_PUBLISHER="_publisher";
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String create= "CREATE TABLE first ("+ BaseColumns._ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+ COL_NAME+" TEXT, "+ COL_PUBLISHER +" TEXT)";
sqLiteDatabase.execSQL(create);
}
答案 1 :(得分:0)
这个问题与分号无关。
无论例外情况如何都会发出未找到表。
实际异常似乎是CursorIndexOutOfBoundsException
,这是因为在尝试检索基础数据之前,游标没有通过Cursor move???
方法移动到行。
moveToFirst
已在下方使用过。)使用的代码: -
public class g_dbHelper extends SQLiteOpenHelper implements BaseColumns {
final static String DB_NAME="books.db";
public g_dbHelper(Context context) {
super(context,DB_NAME, null, 2);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String create= "CREATE TABLE first ("+ BaseColumns._ID+" INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, PUBLISHER TEXT);";
sqLiteDatabase.execSQL(create);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
}
}
public class MainActivity extends AppCompatActivity {
g_dbHelper helper;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView) this.findViewById(R.id.textviewa);
SO48472717();
}
private void SO48472717() {
helper = new g_dbHelper((this));
SQLiteDatabase sql = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("NAME", "Harry Potter");
values.put("PUBLISHER", "Bloomberg Publications");
long newRowId = sql.insert("first", null, values);
String[] name = {"NAME"};
SQLiteDatabase sql1 = helper.getReadableDatabase();
Cursor cursor = sql1.query("first", name, null, null, null, null, null);
//########################### Fix Here.
if (cursor.moveToFirst()) {
int id = cursor.getColumnIndex("NAME");
txt.setText(cursor.getString(id));
}
//########################### End of Fix
cursor.close();
}
}
if(cursor.moveToFirst) {....}
的使用,如果没有这个,你会得到: -
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1