我的应用程序通过从sqlite数据库获取记录完美地工作,但突然它没有给我这样的表,sqlite异常。 请帮我弄清楚问题。
SqliteListHelper2.java
public class SqliteDatabaseHelper2 {
DbListHelper2 helper2;
public SqliteDatabaseHelper2(Context context) {
helper2 = new DbListHelper2(context);
}
public SqliteDatabaseHelper2 open(Context context) throws SQLException {
helper2 = new DbListHelper2(context);
SQLiteDatabase db2 = helper2.getWritableDatabase();
return this;
}
public void close() {
if (helper2 != null) {
helper2.close();
}
}
public long insertData2(String artworkImage, String wrapperType, String
artistName, String collectionName,
String trackName, String collectionPrice, String trackId) {
SQLiteDatabase db2 = helper2.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DbListHelper2.ARTWORK_IMAGE, artworkImage);
values.put(DbListHelper2.WRAPPER_TYPE, wrapperType);
values.put(DbListHelper2.ARTIST_NAME, artistName);
values.put(DbListHelper2.COLLECTION_NAME, collectionName);
values.put(DbListHelper2.TRACK_NAME, trackName);
values.put(DbListHelper2.COLLECTION_PRICE, collectionPrice);
values.put(DbListHelper2.TRACK_ID, trackId);
long id2 = db2.insert(DbListHelper2.TABLE_NAME, null, values);
return id2;
}
public Cursor getAllData2() {
SQLiteDatabase db2 = helper2.getWritableDatabase();
String[] columns = { DbListHelper2.UID, DbListHelper2.ARTWORK_IMAGE,
DbListHelper2.WRAPPER_TYPE,
DbListHelper2.ARTIST_NAME, DbListHelper2.COLLECTION_NAME,
DbListHelper2.TRACK_NAME,
DbListHelper2.COLLECTION_PRICE, DbListHelper2.TRACK_ID };
Cursor cursor2 = db2.query(DbListHelper2.TABLE_NAME, columns, null,
null, null, null, null);
StringBuffer buffer = new StringBuffer();
while (cursor2.moveToNext()) {
int index = cursor2.getColumnIndex(DbListHelper2.UID);
int cid = cursor2.getInt(index);
int index0 =
cursor2.getColumnIndex(DbListHelper2.ARTWORK_IMAGE);
String cid0 = cursor2.getString(index0);
int index1 = cursor2.getColumnIndex(DbListHelper2.WRAPPER_TYPE);
String cid1 = cursor2.getString(index1);
int index2 = cursor2.getColumnIndex(DbListHelper2.ARTIST_NAME);
String cid2 = cursor2.getString(index2);
int index3 =
cursor2.getColumnIndex(DbListHelper2.COLLECTION_NAME);
String cid3 = cursor2.getString(index3);
int index4 = cursor2.getColumnIndex(DbListHelper2.TRACK_NAME);
String cid4 = cursor2.getString(index4);
int index5 =
cursor2.getColumnIndex(DbListHelper2.COLLECTION_PRICE);
String cid5 = cursor2.getString(index5);
int index6 = cursor2.getColumnIndex(DbListHelper2.TRACK_ID);
String cid6 = cursor2.getString(index6);
buffer.append(cid + " " + cid0 + " " + cid1 + " " + cid2 + " " +
cid3 + " " + cid4 + " " + cid5 + " "
+ cid6 + "\n");
}
return cursor2;
}
static class DbListHelper2 extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "Tracks";
private static final String TABLE_NAME = "SingleTrackList";
private static final int DATABASE_VERSION = 1;
private Context context;
static final String UID = "_id";
static final String ARTWORK_IMAGE = "artworkUrl100";
static final String WRAPPER_TYPE = "Wrapper_Type";
static final String ARTIST_NAME = "artistName";
static final String COLLECTION_NAME = "collectionName";
static final String TRACK_NAME = "trackName";
static final String COLLECTION_PRICE = "collectionPrice";
static final String TRACK_ID = "trackId";
private static final String CREATE_TABLE = "Create table "
+TABLE_NAME+" (" +UID
+" INTEGER PRIMARY KEY AUTOINCREMENT, "+ARTWORK_IMAGE+"
VARCHAR(500), "+WRAPPER_TYPE
+" VARCHAR(50), "+ARTIST_NAME+ " VARCHAR(500),
"+COLLECTION_NAME+" VARCHAR(500), "
+TRACK_NAME+" VARCHAR(500), "+COLLECTION_PRICE+"
VARCHAR(500), "+TRACK_ID
+" VARCHAR(255))";
private static final String DROP_TABLE = "Drop table If Exists "
+TABLE_NAME;
public DbListHelper2(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
Message.message(context, "Constructor called");
}
@Override
public void onCreate(SQLiteDatabase db2) {
try {
Message.message(context, "onCreate called");
db2.execSQL(CREATE_TABLE);
} catch (SQLException e) {
Message.message(context, "onCreate failed");
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db2, int oldVersion, int
newVersion) {
try {
Message.message(context, "onUpgrage called");
db2.execSQL(DROP_TABLE);
onCreate(db2);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
SingleTuneDetails.java:这是我将json数据插入我的sqlite数据库的类。
public class SingleTunesDetails extends Activity {
private SqliteDatabaseHelper2 listHelper2;
@Override
protected void onCreate(Bundle savedInstanceState) {
listHelper2 = new SqliteDatabaseHelper2(getBaseContext());
listHelper2.open(getBaseContext());
url = "https://itunes.apple.com/lookup?id=" + passedData7;
// Calling async task to get json
new GetSingleTrackDetails().execute();
}
class GetSingleTrackDetails extends AsyncTask<Void, Void, Void> {
private JSONObject jsonObj;
private Cursor cursor;
@Override
protected Void doInBackground(Void... params) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
tracks = jsonObj.getJSONArray(TAG_RESULT);
// looping through All Products
for (int i = 0; i < tracks.length(); i++) {
JSONObject c = tracks.getJSONObject(i);
artworkImage = c.getString("artworkUrl100");
wrapperType = c.getString("wrapperType");
artistName = c.getString("artistName");
collectionName = c.getString("collectionName");
trackName = c.getString("trackName");
collectionPrice = c.getString("collectionPrice");
trackId = c.getString("trackId");
// tmp hashmap for single contact
HashMap<String, String> tunesMap = new HashMap<String,
String>();
// adding each child node to HashMap key => value
// contact.put(TAG_ID, firstname);
tunesMap.put(TAG_ARTWORK_IMAGE, artworkImage);
tunesMap.put(TAG_WRAPPER_TYPE, wrapperType);
tunesMap.put(TAG_ARTIST_NAME, artistName);
tunesMap.put(TAG_COLLECTION_NAME, collectionName);
tunesMap.put(TAG_TRACK_NAME, trackName);
tunesMap.put(TAG_COLLECTION_PRICE, collectionPrice);
tunesMap.put(TAG_TRACK_ID, trackId);
// adding contact to contact list
singleTrackDetails.add(tunesMap);
long id = listHelper2.insertData2(artworkImage,
wrapperType, artistName, collectionName,
trackName, collectionPrice, trackId);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
cursor = listHelper2.getAllData2();
if (cursor.moveToFirst()) {
do{
wrapperTypeText.setText(passedData2);
artistNameText.setText(passedData3);
collectionNameText.setText(passedData4);
trackNameText.setText(passedData5);
collectionPriceText.setText(passedData6);
}while(cursor.moveToNext());
}
cursor.close();
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
listHelper2.close();
}
}
Logcat:
05-19 12:40:37.996:E / SQLiteLog(3236):( 1)没有这样的表:SingleTrackList 05-19 12:40:38.016:E / SQLiteDatabase(3236):插入trackId = 879273565 trackName = Better Together Wrapper_Type = track artistName = Jack Johnson artworkUrl100 = http://is4.mzstatic.com/image/pf/us/r30/Music4/v4/41/df/6f/41df6fb5-d08f-5573-fb4b-a56a9b6ea0cb/UMG_cvrart_00602537868858_01_RGB72_900x810_06UMGIM25847.100x100-75.jpg collectionName = In Between Dreams collectionPrice = 9.99 05-19 12:40:38.016:E / SQLiteDatabase(3236):android.database.sqlite.SQLiteException:没有这样的表:SingleTrackList(代码1):,同时编译:INSERT INTO SingleTrackList(trackId,trackName,Wrapper_Type,artistName, artworkUrl100,collectionName,collectionPrice)VALUES(?,?,?,?,?,?,?) 05-19 12:40:38.016:E / SQLiteDatabase(3236):at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 05-19 12:40:38.016:E / SQLiteDatabase(3236):在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:909) 05-19 12:40:38.016:E / SQLiteDatabase(3236):在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:520) 05-19 12:40:38.016:E / SQLiteDatabase(3236):在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 05-19 12:40:38.016:E / SQLiteDatabase(3236):在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58) 05-19 12:40:38.016:E / SQLiteDatabase(3236):在android.database.sqlite.SQLiteStatement。(SQLiteStatement.java:31) 05-19 12:40:38.016:E / SQLiteDatabase(3236):在android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1523) 05-19 12:40:38.016:E / SQLiteDatabase(3236):在android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1395) 05-19 12:40:38.016:E / SQLiteDatabase(3236):at com.example.fasoostest.SqliteDatabaseHelper2.insertData2(SqliteDatabaseHelper2.java:41) 05-19 12:40:38.016:E / SQLiteDatabase(3236):at com.example.fasoostest.SingleTunesDetails $ GetSingleTrackDetails.doInBackground(SingleTunesDetails.java:135) 05-19 12:40:38.016:E / SQLiteDatabase(3236):at com.example.fasoostest.SingleTunesDetails $ GetSingleTrackDetails.doInBackground(SingleTunesDetails.java:1) 05-19 12:40:38.016:E / SQLiteDatabase(3236):在android.os.AsyncTask $ 2.call(AsyncTask.java:287) 05-19 12:40:38.016:E / SQLiteDatabase(3236):at java.util.concurrent.FutureTask.run(FutureTask.java:234) 05-19 12:40:38.016:E / SQLiteDatabase(3236):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230) 05-19 12:40:38.016:E / SQLiteDatabase(3236):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 05-19 12:40:38.016:E / SQLiteDatabase(3236):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:573) 05-19 12:40:38.016:E / SQLiteDatabase(3236):at java.lang.Thread.run(Thread.java:864) 05-19 12:40:38.021:E / SQLiteLog(3236):( 1)没有这样的表:SingleTrackList 05-19 12:40:38.021:W / dalvikvm(3236):threadid = 1:线程退出未捕获异常(组= 0x40d47a08) 05-19 12:40:38.036:E / AndroidRuntime(3236):致命异常:主要 05-19 12:40:38.036:E / AndroidRuntime(3236):android.database.sqlite.SQLiteException:没有这样的表:SingleTrackList(代码1):,同时编译:SELECT _id,artworkUrl100,Wrapper_Type,artistName,collectionName,trackName ,collectionPrice,trackId FROM SingleTrackList 05-19 12:40:38.036:E / AndroidRuntime(3236):在android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 05-19 12:40:38.036:E / AndroidRuntime(3236):在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:909) 05-19 12:40:38.036:E / AndroidRuntime(3236):在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:520) 05-19 12:40:38.036:E / AndroidRuntime(3236):在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 05-19 12:40:38.036:E / AndroidRuntime(3236):在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58) 05-19 12:40:38.036:E / AndroidRuntime(3236):在android.database.sqlite.SQLiteQuery。(SQLiteQuery.java:37) 05-19 12:40:38.036:E / AndroidRuntime(3236):在android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:65) 05-19 12:40:38.036:E / AndroidRuntime(3236):在android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1370) 05-19 12:40:38.036:E / AndroidRuntime(3236):在android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1217) 05-19 12:40:38.036:E / AndroidRuntime(3236):在android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1088) 05-19 12:40:38.036:E / AndroidRuntime(3236):在android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1256) 05-19 12:40:38.036:E / AndroidRuntime(3236):at com.example.fasoostest.SqliteDatabaseHelper2.getAllData2(SqliteDatabaseHelper2.java:51) 05-19 12:40:38.036:E / AndroidRuntime(3236):at com.example.fasoostest.SingleTunesDetails $ GetSingleTrackDetails.onPostExecute(SingleTunesDetails.java:154) 05-19 12:40:38.036:E / AndroidRuntime(3236):at com.example.fasoostest.SingleTunesDetails $ GetSingleTrackDetails.onPostExecute(SingleTunesDetails.java:1) 05-19 12:40:38.036:E / AndroidRuntime(3236):在android.os.AsyncTask.finish(AsyncTask.java:631) 05-19 12:40:38.036:E / AndroidRuntime(3236):在android.os.AsyncTask.access $ 600(AsyncTask.java:177) 05-19 12:40:38.036:E / AndroidRuntime(3236):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:644) 05-19 12:40:38.036:E / AndroidRuntime(3236):在android.os.Handler.dispatchMessage(Handler.java:99) 05-19 12:40:38.036:E / AndroidRuntime(3236):在android.os.Looper.loop(Looper.java:158) 05-19 12:40:38.036:E / AndroidRuntime(3236):在android.app.ActivityThread.main(ActivityThread.java:5751) 05-19 12:40:38.036:E / AndroidRuntime(3236):at java.lang.reflect.Method.invokeNative(Native Method) 05-19 12:40:38.036:E / AndroidRuntime(3236):at java.lang.reflect.Method.invoke(Method.java:511) 05-19 12:40:38.036:E / AndroidRuntime(3236):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1083) 05-19 12:40:38.036:E / AndroidRuntime(3236):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850) 05-19 12:40:38.036:E / AndroidRuntime(3236):at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
我有两个表一个显示listview,其他表是这个,我的第一个表不是给我任何错误,但我的第二个表给了我sqlite异常
您发布的代码仅显示正在创建的一个表。
每个数据库文件只使用一个SQLiteOpenHelper
。第二个帮助程序只是发现数据库文件已经存在并且具有正确的版本,因此不需要调用onCreate()
/ onUpgrade()
生命周期回调。见intarray extension
只需将创建两个表的SQL放在与onCreate()
两个execSQL()
个调用相同的catch
中。
另请考虑删除帮助器onCreate()
和onUpgrade()
中的所有((SubClass)s).subClass();
块。语法问题需要向上抛出。
还有一个空白问题导致M D在他删除的答案中发现“没有这样的专栏”。