对于我的应用程序,我创建了 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)