我正在创建一个非常基本的android数据库应用程序,但尝试超出我使用SQLiteOpenHelper创建的数据库的活动正在捕获SQLiteException并创建一个“找不到数据库”的toast。以下是我的工作
超出数据库的活动
package com.hfad.starbuzz;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class DrinkCategoryActivity extends ListActivity {
private Cursor cursor;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView listview = getListView();
try{
SQLiteOpenHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(this);
db = starbuzzDatabaseHelper.getReadableDatabase();
cursor = db.query("Drink",new String[]{"_id","NAME"},null,null,null,null,null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,
cursor,new String[]{"NAME"},new int[]{android.R.id.text1},0);
listview.setAdapter(adapter);
}
catch(SQLiteException e){
Toast.makeText(this, "Database not found", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent(DrinkCategoryActivity.this,DrinkActivity.class);
intent.putExtra(DrinkActivity.EXTRADRINKO,(int) id);
startActivity(intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
cursor.close();
db.close();
}
}
数据库帮助程序代码
package com.hfad.starbuzz;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by user on 11/7/2016.
*/
public class StarbuzzDatabaseHelper extends SQLiteOpenHelper {
private static final int DB_VERSION = 1;
private static final String DB_NAME = "Starbuzz";
public StarbuzzDatabaseHelper(Context context){
super(context,DB_NAME,null,DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
updateDatabase(db,0,DB_VERSION);
}
private void insert(String name , String description , int id , SQLiteDatabase db){
ContentValues drinkValues = new ContentValues();
drinkValues.put("NAME",name);
drinkValues.put("DESCRIPTION",description);
drinkValues.put("IMAGE_RESOURCE_ID",id);
db.insert("DRINK", null , drinkValues);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
updateDatabase(db,oldVersion,newVersion);
}
private void updateDatabase(SQLiteDatabase db , int oldVersion , int newVersion){
db.execSQL("create table DRINK(" +
"_id primary key INTEGER AUTOINCREMENT,"
+ "NAME TEXT,"
+"DESCRIPTION TEXT,"
+"IMAGE_RESOURCE_ID INTEGER);");
insert("latte","Espresso and steamed milk",R.drawable.latte,db);
insert("cappuccino","Espresso and steamed milk and lollypop for u to suck, SUCKER",R.drawable.cappuccino,db);
insert("filter","to filter your ass off because it is fucking dirty",R.drawable.filter,db);
}
}
答案 0 :(得分:-2)
试试这个
private static DatabaseHelper ourInstance;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public static DatabaseHelper getInstance(Context context) {
if (ourInstance == null)
ourInstance = new DatabaseHelper(context);
ourInstance.getReadableDatabase();
return ourInstance;
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE_TODO = " CREATE TABLE " + TABLE_NAME + "(" + TASK_ID +
" INTEGER PRIMARY KEY," + TASK_NAME + " TEXT, " + TASK_TIME + " TEXT, " + TASK_DATE + " TEXT " + " ) ";
db.execSQL(CREATE_TABLE_TODO);
}