0

对于我的应用程序,我创建了 sqlite 数据库。我添加了添加和检索数据的功能。我可以看到数据添加 logcat 消息。但是当接收到数据时,它说没有找到数据。我的数据库代码如下

package com.burusoth1990.advertise;

import java.util.ArrayList;
import java.util.List;

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

public class DatabaseHandler extends SQLiteOpenHelper
{

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME="ad_details";
    private static final String TABLE_NAME="advertisement";

    private static final String KEY_HEADING="heading";
    private static final String KEY_SRC="src";
    private static final String KEY_TYPE="type";
    private static final String KEY_DISTRICT="district";
    private static final String KEY_MORE="more";
    private static final String KEY_REF_ID="id";


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

    //create table for advertisements
    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        String CREATE_AD_TABLE = "CREATE TABLE IF NOT EXIST " + TABLE_NAME + "(" +KEY_REF_ID+ "TEXT PRIMARY KEY,"+ KEY_HEADING + " TEXT," + KEY_DISTRICT + " TEXT,"+  KEY_TYPE + " TEXT" +KEY_SRC + " TEXT" +  KEY_MORE+ " TEXT" + ")";
        db.execSQL(CREATE_AD_TABLE);
    }

    //upgrade a existing table
    @Override
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) 
    {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    //add a advertisement into database
    public void add_ad(advertisements advertisement)
    {
        SQLiteDatabase db=this.getWritableDatabase();   
        ContentValues values=new ContentValues();
        values.put(KEY_REF_ID, advertisement._id);
        values.put(KEY_HEADING, advertisement._heading);
        values.put(KEY_DISTRICT, advertisement._district);
        values.put(KEY_TYPE, advertisement._type);
        values.put(KEY_SRC, advertisement._src);
        values.put(KEY_MORE, advertisement._more);

        db.insert(TABLE_NAME, null, values);
        Log.e("ADDED","data added to database");
        db.close();
    }

    //get a specific advertisement
    public advertisements get_ad(int ref_id)
    {
        return null;
    }

    //get a advertisement from database
    public List<advertisements> get_all()
    {
        List<advertisements> ad_list=new ArrayList<advertisements>();
        String select_query="SELECT * FROM "+TABLE_NAME;

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

        if(cursor.moveToFirst())
        {
            do{
                advertisements ad=new advertisements();
                ad.set_id(cursor.getString(0));
                ad.set_heading(cursor.getString(1));
                ad.set_district(cursor.getString(2));
                ad.set_type(cursor.getString(3));
                ad.set_src(cursor.getString(4));
                ad.set_more(cursor.getString(5));
                Log.e("DATABASE",cursor.getString(5));
                ad_list.add(ad);
            }
            while(cursor.moveToNext());
        }


        return ad_list;
    }

    //delete a advertisement from database
    public void delete_ad()
    {

    }

    //get number of advertisements in the database
    public int get_count()
    {
        String count_query="SELECT * FROM "+TABLE_NAME;
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor cursor=db.rawQuery(count_query, null);
        cursor.close();
        db.close();

        return cursor.getColumnCount();
    }

}

我的 logcat 在插入时说错误

10-09 20:21:34.482: E/SQLiteDatabase(2778): Error inserting id=1000 more=s/s src=hi hi type=funny district=colombo heading=Testing
10-09 20:21:34.482: E/SQLiteDatabase(2778): android.database.sqlite.SQLiteException: table advertisement has no column named id (code 1): , while compiling: INSERT INTO advertisement(id,more,src,type,district,heading) VALUES (?,?,?,?,?,?)
10-09 20:21:34.482: E/SQLiteDatabase(2778):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
10-09 20:21:34.482: E/SQLiteDatabase(2778):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
10-09 20:21:34.482: E/SQLiteDatabase(2778):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
10-09 20:21:34.482: E/SQLiteDatabase(2778):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
10-09 20:21:34.482: E/SQLiteDatabase(2778):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
10-09 20:21:34.482: E/SQLiteDatabase(2778):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
10-09 20:21:34.482: E/SQLiteDatabase(2778):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
10-09 20:21:34.482: E/SQLiteDatabase(2778):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
10-09 20:21:34.482: E/SQLiteDatabase(2778):     at com.burusoth1990.advertise.DatabaseHandler.add_ad(DatabaseHandler.java:64)
10-09 20:21:34.482: E/SQLiteDatabase(2778):     at com.burusoth1990.advertise.Gadget_view.onCreate(Gadget_view.java:45)
10-09 20:21:34.482: E/SQLiteDatabase(2778):     at android.app.Activity.performCreate(Activity.java:5104)
10-09 20:21:34.482: E/SQLiteDatabase(2778):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
4

4 回答 4

1

您缺少一个空格:

改变:

"CREATE TABLE IF NOT EXIST " + TABLE_NAME + "(" +KEY_REF_ID+ "TEXT PRIMARY KEY,"

至:

"CREATE TABLE IF NOT EXIST " + TABLE_NAME + "(" +KEY_REF_ID+ " TEXT PRIMARY KEY,"

                                           Missing Space here ^
于 2013-10-09T20:32:24.417 回答
1

我在这些文本之间看不到任何空格。

"(" +KEY_REF_ID+ "TEXT PRIMARY KEY,"+

它连接为idTEXT PRIMARY KEY

其次,您可以尝试在没有这些常量的情况下编写创建字符串。

private static final String KEY_HEADING="heading";
    private static final String KEY_SRC="src";
    private static final String KEY_TYPE="type";
    private static final String KEY_DISTRICT="district";
    private static final String KEY_MORE="more";
    private static final String KEY_REF_ID="id";

像这样写; "CREATE TABLE IF NOT EXIST advertisement (id ... ,src ... , type ...) ";

于 2013-10-09T20:38:09.107 回答
0

首先,通过将所有字段名称放在静态常量中,使 SQL 字符串变得更加复杂。

当您执行 .getWritableDatabase 时,将调用您的数据库 oncreate。将调用移至 OpenHelper 构造函数,因为它当前位于添加数据的函数中,但该函数未使用。

在您的 openhelper 类中保留对数据库的引用,而不是在每个数据函数中重新创建。

于 2013-10-09T18:34:45.597 回答
-1

首先你有

...+KEY_REF_ID + "TEXT PRIMARY KEY"...

这是一个文本主键,您应该替换为以下内容

...+KEY_REF_ID + "INTEGER PRIMARY KEY"...  

无论如何,这是我的建议,它可能会有所帮助。

于 2013-10-09T18:03:24.813 回答