java - 拒绝对先前失败的类 java.lang.Class 重新初始化android studio sqlite 数据库
问题描述
`package com.example.wheresnapped;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataManager {
// the actual apps database
private SQLiteDatabase db;
// public static final string for each row of the table that we need
// to refer both inside and outside this class
public static final String TABLE_ROW_ID = "_id";
public static final String TABLE_ROW_TITLE = "image_title";
public static final String TABLE_ROW_URI = "image_uri";
// private ones that we need to refer to just from within this class
private static final String DB_NAME = "w_snap_db";
private static final int DB_VERSION = 1;
private static final String TABLE_PHOTOS = "ws_table_photos";
private static final String TABLE_TAGS = "ws_table_tags";
private static final String TABLE_ROW_TAG1 = "tag1";
private static final String TABLE_ROW_TAG2 = "tag2";
private static final String TABLE_ROW_TAG3 = "tag3";
public static final String TABLE_ROW_TAG = "tag"; // for the tags table
public DataManager(Context context){
CustomSQLITEOpenHelper helper = new CustomSQLITEOpenHelper(context);
// get a writable database
db = helper.getWritableDatabase();
}
public void addPhoto(Photo photo){
// add all the details to the photos table
String query = "INSERT INTO " + TABLE_PHOTOS +" (" +
TABLE_ROW_TITLE + ", " +
TABLE_ROW_URI + ", " +
TABLE_ROW_TAG1 + ", " +
TABLE_ROW_TAG2 + ", " +
TABLE_ROW_TAG3 + ") " +
"VALUES (" +
"'" + photo.getTitle() +"'" + ", " +
"'" + photo.getStorageLocation() + "'" + ", " +
"'" + photo.getTag1() + "'" + ", " +
"'" + photo.getTag2() + "'" + ", " +
"'" + photo.getTag3() + "'" + ", " +
");";
Log.i("addPhoto()", query);
db.execSQL(query);
// add any new tags to the tags table
query = "INSERT INTO " + TABLE_TAGS + "(" +
TABLE_ROW_TAG + ") " +
"SELECT '" + photo.getTag1() + "' " +
"WHERE NOT EXISTS (SELECT 1 FROM )" +
TABLE_TAGS +
" WHERE " + TABLE_ROW_TAG + " = " +
"'" + photo.getTag1() +"');";
db.execSQL(query);
query = "INSERT INTO " + TABLE_TAGS + "(" +
TABLE_ROW_TAG + ") " +
"SELECT '" + photo.getTag2() + "' " +
"WHERE NOT EXISTS (SELECT 1 FROM )" +
TABLE_TAGS +
" WHERE " + TABLE_ROW_TAG + " = " +
"'" + photo.getTag2() +"');";
db.execSQL(query);
query = "INSERT INTO " + TABLE_TAGS + "(" +
TABLE_ROW_TAG + ") " +
"SELECT '" + photo.getTag3() + "' " +
"WHERE NOT EXISTS (SELECT 1 FROM )" +
TABLE_TAGS +
" WHERE " + TABLE_ROW_TAG + " = " +
"'" + photo.getTag3() +"');";
db.execSQL(query);
}
public Cursor getTitles(){
Cursor c = db.rawQuery("SELECT " + TABLE_ROW_ID + ", " +
TABLE_ROW_TITLE + "from " + TABLE_PHOTOS, null);
c.moveToFirst();
return c;
}
public Cursor getTitlesWithTag(String tag){
Cursor c = db.rawQuery("SELECT " + TABLE_ROW_ID + ", " +
TABLE_ROW_TITLE + " from " +
TABLE_PHOTOS + " WHERE " +
TABLE_ROW_TAG1 + " = '" + tag + "' or " +
TABLE_ROW_TAG2 + " = '" + tag + "' or " +
TABLE_ROW_TAG3 + " = '" + tag + "';", null);
c.moveToFirst();
return c;
}
public Cursor getPhoto(int id){
Cursor c = db.rawQuery("SELECT * from " +
TABLE_PHOTOS +
" WHERE " +
TABLE_ROW_ID + " = " + id, null);
c.moveToFirst();
return c;
}
// returns all the tags for our fragment class
public Cursor getTags(){
Cursor c = db.rawQuery("SELECT " + TABLE_ROW_ID + ", " +
TABLE_ROW_TAG + " from " + TABLE_TAGS, null);
c.moveToFirst();
return c;
}
// class is created when our DataManager is initialized
private class CustomSQLITEOpenHelper extends SQLiteOpenHelper {
public CustomSQLITEOpenHelper(Context context){
super(context, DB_NAME, null, DB_VERSION);
}
// only runs the first time the database is created
@Override
public void onCreate(SQLiteDatabase db) {
// create table for photos and all their details
String newTableQueryString = "create table "
+ TABLE_PHOTOS + " ("
+TABLE_ROW_ID
+ " integer primary key autoincrement not null,"
+ TABLE_ROW_TITLE
+ " text not null,"
+ TABLE_ROW_URI
+ " text not null,"
+ TABLE_ROW_TAG1
+ " text not null,"
+ TABLE_ROW_TAG2
+ " text not null,"
+ TABLE_ROW_TAG3
+ " text not null" + ");";
db.execSQL(newTableQueryString);
// create a separate table for tags
newTableQueryString = "create table "
+ TABLE_TAGS + " ("
+ TABLE_ROW_ID
+ " integer primary key autoincrement not null,"
+ TABLE_ROW_TAG
+ " text not null" + ");";
db.execSQL(newTableQueryString);
}
// method only runs when we increment db version
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}
我正在开发一个从 sqlite 数据库 android 获取数据的应用程序,我的代码没有错误或警告,但应用程序在启动时崩溃,日志窗口上显示图像上的消息。我搜索了与 sql 相关的错误,但似乎找不到与我还附加的数据库创建代码有关的任何错误。对此的任何帮助都将不胜感激
解决方案
推荐阅读
- regex - 用于删除单词中特殊字符的正则表达式
- c - 谁调用了鼠标驱动程序的 open()?
- java - 为什么将 Avro 与 Kafka 一起使用 - 如何处理 POJO
- angular - 这个日志文件有什么问题。有人可以指点我正确的方向吗?
- c++ - 为什么移动运算符不只是析构函数+移动构造函数
- c++ - 为什么 boost 期货返回不同的值?
- python - PyPDF2 有哪些替代方法来管理 PDF 文件?
- node.js - 伊斯坦布尔测试覆盖率信息
- javascript - 为什么代码不起作用 - Adobe LiveCycle Designer - JavaScript
- asp.net - 在 XHR 请求上随机出现 ERR_SPDY_PROTOCOL_ERROR