对于我的应用程序,我创建了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)
答案 0 :(得分: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 ^
答案 1 :(得分: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 ...) ";
答案 2 :(得分:0)
首先,通过将所有字段名称放在静态常量中,使SQL字符串变得更加复杂。
执行.getWritableDatabase时将调用您的数据库oncreate。将调用移动到您的OpenHelper构造函数,因为它当前在您的函数中添加数据但该函数未被使用。
在openhelper类中保留对数据库的引用,而不是在每个数据函数中重新创建。
答案 3 :(得分:-1)
首先,你有
...+KEY_REF_ID + "TEXT PRIMARY KEY"...
这是一个文本主键,您应该替换为以下
...+KEY_REF_ID + "INTEGER PRIMARY KEY"...
无论如何,我的建议可能有所帮助。