2014-12-06 29 views
0

請忽略我的經驗不足,因爲這使用這個,非常新的編碼是第一次,但我不斷收到此錯誤:android.database.sqlite:SQLiteException:沒有這樣的表存在錯誤持續出現

「所致。 SQLiteException:沒有這樣的表:照片(代碼1):,編譯時:SELECT * FROM照片)「

請幫忙!

import java.util.ArrayList; 
import java.util.HashMap; 

import android.util.Log; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class DBController extends SQLiteOpenHelper { 
    private static final String LOGCAT = null; 

    public DBController(Context applicationcontext) { 
     super(applicationcontext, "androidsqlite.db", null, 1); 
     Log.d(LOGCAT,"Created"); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase database) { 
     String query; 
     query = "CREATE TABLE photo (photoId INTEGER PRIMARY KEY, photoName TEXT)"; 
     database.execSQL(query); 
     Log.d(LOGCAT,"photo Created"); 
    } 
    @Override 
    public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) { 
     String query; 
     query = "DROP TABLE IF EXISTS photo"; 
     database.execSQL(query); 
     onCreate(database); 
    } 

    public void insertPhoto (HashMap<String, String> queryValues) { 
     SQLiteDatabase database = this.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     values.put("photoName", queryValues.get("photoName")); 
     database.insert("photo", null, values); 
     database.close(); 
    } 

    public int updatePhoto (HashMap<String, String> queryValues) { 
     SQLiteDatabase database = this.getWritableDatabase();  
     ContentValues values = new ContentValues(); 
     values.put("photoName", queryValues.get("photoName")); 
     return database.update("photo", values, "photoId" + " = ?", new String[] { queryValues.get("photoId") }); 
     //String updateQuery = "Update words set txtWord='"+word+"' where txtWord='"+ oldWord +"'"; 
     //Log.d(LOGCAT,updateQuery); 
     //database.rawQuery(updateQuery, null); 
     //return database.update("words", values, "txtWord = ?", new String[] { word }); 
    } 

    public void deletePhoto(String id) { 
     Log.d(LOGCAT,"delete"); 
     SQLiteDatabase database = this.getWritableDatabase();  
     String deleteQuery = "DELETE FROM photo where photoId='"+ id +"'"; 
     Log.d("query",deleteQuery);  
     database.execSQL(deleteQuery); 
    } 

    public ArrayList<HashMap<String, String>> getAllPhoto() { 
     ArrayList<HashMap<String, String>> wordList; 
     wordList = new ArrayList<HashMap<String, String>>(); 
     String selectQuery = "SELECT * FROM photo"; 
     SQLiteDatabase database = this.getWritableDatabase(); 
     Cursor cursor = database.rawQuery(selectQuery, null); 
     if (cursor.moveToFirst()) { 
      do { 
       HashMap<String, String> map = new HashMap<String, String>(); 
       map.put("photoId", cursor.getString(0)); 
       map.put("photoName", cursor.getString(1)); 
       wordList.add(map); 
      } while (cursor.moveToNext()); 
     } 

     // return contact list 
     return wordList; 
    } 

    public HashMap<String, String> getPhotoInfo(String id) { 
     HashMap<String, String> wordList = new HashMap<String, String>(); 
     SQLiteDatabase database = this.getReadableDatabase(); 
     String selectQuery = "SELECT * FROM photo where photoId='"+id+"'"; 
     Cursor cursor = database.rawQuery(selectQuery, null); 
     if (cursor.moveToFirst()) { 
      do { 
        //HashMap<String, String> map = new HashMap<String, String>(); 
       wordList.put("photoName", cursor.getString(1)); 
        //wordList.add(map); 
      } while (cursor.moveToNext()); 
     }     
    return wordList; 
    } 
} 
+0

你得到「照片創建」在logcat中調用'getAllPhoto'過嗎? – TWiStErRob 2014-12-06 11:28:05

+0

沒有答案。只是爲了通知**另一個問題**:'String deleteQuery =「DELETE FROM photo where photoId ='」+ id +「'」;'是錯誤的:id是一個整數,它不想**'**圍繞其價值。 – 2014-12-06 11:35:54

+0

只需從設備或模擬器卸載您的應用程序,然後再次嘗試安裝應用程序,這可能會工作 – 2014-12-06 11:41:55

回答

0

enter code here SQLiteDatabase數據庫= this.getReadableDatabase(); 這條線之前請初始化第一數據庫以下行

使用初始化數據庫

SQLiteDatabase database = new SQLiteDatabase(); 
你的情況

這將是DBController database = new DBController ();

DBController database = getReadableDatabase(); 
+0

我仍然沒有成功...除非我輸入它不正確 – user3651285 2014-12-06 12:32:57

0

我用這下面的方法來檢查方法爲Sqlite表。如果表不存在,那麼創建它。你有你的情況來處理這個 -

public boolean isTableExists(String tableName, boolean openDb) { 
    if(openDb) { 
     if(mDatabase == null || !mDatabase.isOpen()) { 
      mDatabase = getReadableDatabase(); 
     } 

     if(!mDatabase.isReadOnly()) { 
      mDatabase.close(); 
      mDatabase = getReadableDatabase(); 
     } 
    } 

    Cursor cursor = mDatabase.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"+tableName+"'", null); 
    if(cursor!=null) { 
     if(cursor.getCount()>0) { 
          cursor.close(); 
      return true; 
     } 
        cursor.close(); 
    } 
    return false; 
} 

乾杯:)

+0

我是否添加我的代碼片段? – user3651285 2014-12-06 12:33:49

相關問題