SQLiteException:不存在此类错误一直发生

时间:2014-12-06 11:21:58

标签: android database sqlite

请忽略我的经验,因为这是第一次使用这个和极其新的编码,但我一直收到这个错误:

“引起:android.database.sqlite.SQLiteException:没有这样的表:照片(代码1):,同时编译:SELECT * FROM photo)”

请帮忙!

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;
    }   
}

2 个答案:

答案 0 :(得分:0)

enter code here SQLiteDatabase database = this.getReadableDatabase(); 在此行之前,请先初始化您的数据库

使用以下行初始化数据库

SQLiteDatabase database = new SQLiteDatabase ();

在您的情况下,它将是DBController database = new DBController ();

 DBController   database = getReadableDatabase();

答案 1 :(得分: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;
}

干杯:)