从Android-sql获取数据

时间:2014-05-08 08:23:47

标签: android android-sqlite

我为android中的照片创建了一个数据库。代码如下。

public class PhotoDataHandler extends SQLiteOpenHelper {
    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 77;

    // Database Name
    private static final String DATABASE_NAME = "photos";

    // DATA table name
    private static final String TABLE_PHOTOS = "PHOTOS";

    // DATA Table Columns names
    private static final String KEY_ID = "id";
    private static final String PHOTO_ID = "photo_id";
    private static final String KEY_JSON = "json";

    public PhotoDataHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_DATA_TABLE = "CREATE TABLE " + TABLE_PHOTOS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_JSON + " TEXT"
                + PHOTO_ID + " TEXT" + ")";
        db.execSQL(CREATE_DATA_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PHOTOS);

        // Create tables again
        onCreate(db);
    }

    // truncate all data
    public void truncateAllData() {
        SQLiteDatabase db = this.getReadableDatabase();

        db.execSQL("DELETE FROM " + TABLE_PHOTOS);
        db.close();
    }

    // Add new DATA
    public void addPhoto(Photo photo) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_JSON, photo.getID());
        values.put(PHOTO_ID, photo.getphotoId());
        values.put(KEY_JSON, photo.getPhotoString());
        // Inserting Row
        db.insert(TABLE_PHOTOS, null, values);
        db.close(); // Closing database connection
    }

    public Photo getLastPhotos() {
        SQLiteDatabase db = this.getReadableDatabase();

        String selectQuery = "SELECT * FROM " + TABLE_PHOTOS
                + " ORDER BY id DESC LIMIT 1";

        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.getCount() != 0) {
            cursor.moveToFirst();
            Photo photo = new Photo(cursor.getString(1));
            db.close();
            return photo;
        } else {
            Photo photo = new Photo();
            db.close();
            return photo;
        }
    }

    // Getting All DATA
    public List<Photo> getAllData() {
        List<Photo> PhotoList = new ArrayList<Photo>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_PHOTOS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Photo photo = new Photo();
                photo.setID(Integer.parseInt(cursor.getString(0)));
                photo.setPhotoString((cursor.getString(1)));

//error is in this line : from logcat
    **photo.setphotoId((Integer.parseInt(cursor.getString(2))));**


                // Adding contact to list
                PhotoList.add(photo);
            } while (cursor.moveToNext());
        }

        // return user list
        db.close();
        return PhotoList;
    }
}

我正在使用此方法添加照片:

final PhotoDataHandler photodb = new PhotoDataHandler(context.getApplicationContext());

photodb.addPhoto(new Photo(61, 8, "String"));
photodb.addPhoto(new Photo(62, 9, "String of photo"));

当我想在数据库中获取所有日期时,我使用sql PhotoDataHandler类中的方法写入:

String alldata = "null";
                List<Photo> photos = photodb.getAllData();
                int nr_photos=0;
                for(Photo x:photos){
                    System.out.println("nr of line : " + nr_photos +" photos id "+x.getID()+ "Stringas yra : "+ x.getPhotoString()+ " dar vienas id yra : " + x.getphotoId()) ;
                    nr_photos++;
                }

每次我使用它时都会感到恐惧。 log cat看起来像这样:

05-08 11:16:52.489: E/SQLiteLog(2256): (1) table PHOTOS has no column named photo_id
05-08 11:16:52.499: E/SQLiteDatabase(2256): Error inserting id=131 photo_id=62 json=vienas
05-08 11:16:52.499: E/SQLiteDatabase(2256): android.database.sqlite.SQLiteException: table PHOTOS has no column named photo_id (code 1): , while compiling: INSERT INTO PHOTOS(id,photo_id,json) VALUES (?,?,?)
05-08 11:16:52.499: E/SQLiteDatabase(2256):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
05-08 11:16:52.499: E/SQLiteDatabase(2256):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
05-08 11:16:52.499: E/SQLiteDatabase(2256):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
05-08 11:16:52.499: E/SQLiteDatabase(2256):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
05-08 11:16:52.499: E/SQLiteDatabase(2256):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
05-08 11:16:52.499: E/SQLiteDatabase(2256):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
05-08 11:16:52.499: E/SQLiteDatabase(2256):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
05-08 11:16:52.499: E/SQLiteDatabase(2256):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
05-08 11:16:52.499: E/SQLiteDatabase(2256):     at com.project.sqlite.PhotoDataHandler.addPhoto(PhotoDataHandler.java:68)
05-08 11:16:52.499: E/SQLiteDatabase(2256):     at com.project.elements.Element$3.onClick(Element.java:730)
05-08 11:16:52.499: E/SQLiteDatabase(2256):     at android.view.View.performClick(View.java:4438)
05-08 11:16:52.499: E/SQLiteDatabase(2256):     at android.view.View$PerformClick.run(View.java:18422)
05-08 11:16:52.499: E/SQLiteDatabase(2256):     at android.os.Handler.handleCallback(Handler.java:733)
05-08 11:16:52.499: E/SQLiteDatabase(2256):     at android.os.Handler.dispatchMessage(Handler.java:95)
05-08 11:16:52.499: E/SQLiteDatabase(2256):     at android.os.Looper.loop(Looper.java:136)
05-08 11:16:52.499: E/SQLiteDatabase(2256):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-08 11:16:52.499: E/SQLiteDatabase(2256):     at java.lang.reflect.Method.invokeNative(Native Method)
05-08 11:16:52.499: E/SQLiteDatabase(2256):     at java.lang.reflect.Method.invoke(Method.java:515)
05-08 11:16:52.499: E/SQLiteDatabase(2256):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-08 11:16:52.499: E/SQLiteDatabase(2256):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-08 11:16:52.499: E/SQLiteDatabase(2256):     at dalvik.system.NativeStart.main(Native Method)

有经验的人可以告诉我这个问题吗?

1 个答案:

答案 0 :(得分:3)

你错了吗

+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_JSON + " TEXT" // forget to add , comma after TEXT

以下

更正
String CREATE_DATA_TABLE = "CREATE TABLE " + TABLE_PHOTOS + "("
            + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_JSON + " TEXT, "
            + PHOTO_ID + " TEXT" + ")";