我在尝试进行查询时遇到错误。这是我的代码:
dbdata.java
public class dbdata
{
private long id;
private String title;
private long lenght;
private String address;
private String desc;
private int nView;
public long getId()
{
return id;
}
public void setId(long id)
{
this.id = id;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public long getlenght()
{
return lenght;
}
public void setlenght(long lenght)
{
this.lenght = lenght;
}
public String getaddres()
{
return address;
}
public void setaddress(String address)
{
this.address = address;
}
public String getdesc()
{
return desc;
}
public void setdesc(String desc)
{
this.desc = desc;
}
public int getnView()
{
return nView;
}
public void setnView(int nView)
{
this.nView = nView;
}
}
MySQLiteHelper.java
public class MySQLiteHelper extends SQLiteOpenHelper
{
public static final String TABLE_Videos = "Video";
public static final String Video_ID = "_id";
public static final String COLUMN_Title= "Title";
public static final String COLUMN_Lenght = "Lenght";
public static final String COLUMN_Address = "Address";
public static final String COLUMN_Desc = "Description";
public static final String COLUMN_NView = "Views";
private static final String DATABASE_NAME = "video.db";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table " + TABLE_Videos +
" ("+ Video_ID + " integer primary key autoincrement, "
+ COLUMN_Title + " text, "
+ COLUMN_Desc + " text, "
+ COLUMN_Address + " text, "
+ COLUMN_Lenght + " text, "
+ COLUMN_NView + " text);";
public MySQLiteHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database)
{
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(MySQLiteHelper.class.getName(), "Upgrading database from version " + oldVersion + " to " + newVersion
+ ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_Videos);
onCreate(db);
}
}
dbdatasource.java
public class dbdatasource
{
// Database fields
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = new String[] {
MySQLiteHelper.Video_ID,
MySQLiteHelper.COLUMN_Title,
MySQLiteHelper.COLUMN_Desc,
MySQLiteHelper.COLUMN_Address };
public dbdatasource(Context context)
{
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException
{
database = dbHelper.getWritableDatabase();
}
public void close()
{
dbHelper.close();
}
public void data(String title, String desc, String address)
{
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_Title, title);
values.put(MySQLiteHelper.COLUMN_Desc, desc);
values.put(MySQLiteHelper.COLUMN_Address, address);
database.insert(MySQLiteHelper.TABLE_Videos, null,values);
}
public List<dbdata> getAllData()
{
List<dbdata> datalist = new ArrayList<dbdata>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_Videos, allColumns, null, null, null, null, null);
if (cursor.moveToFirst())
{
do
{
dbdata data = cursorToData(cursor);
datalist.add(data);
} while(cursor.moveToNext());
}
// make sure to close the cursor
cursor.close();
return datalist;
}
private dbdata cursorToData(Cursor cursor)
{
dbdata data = new dbdata();
data.setId(cursor.getLong(0));
data.setTitle(cursor.getString(1));
data.setdesc(cursor.getString(2));
data.setaddress(cursor.getString(3));
data.setlenght(cursor.getLong(4));
data.setnView(cursor.getInt(5));
return data;
}
}
这些是类main中与db
“交谈”的方法private dbdatasource datasource;
datasource = new dbdatasource(this);
datasource.open();
protected void dbread()
{
List<dbdata> values = new ArrayList<dbdata>();
values = datasource.getAllData();
for(int i=0;i<values.size(); i++)
{
list.addlist(values.get(i).getTitle(),values.get(i).getdesc());
}
}
protected void dbwrite(String t, String d, String a)
{
datasource.data(t,d,a);
}
当我编译代码时,我得到了错误:
no such column: Description (code 1): , while compiling: SELECT _id, Title, Description, Address FROM Video
这是logcat
12-03 14:54:39.466: D/dalvikvm(10483): Late-enabling CheckJNI
12-03 14:54:39.474: E/Trace(10483): error opening trace file: No such file or directory (2)
12-03 14:54:39.486: E/SQLiteLog(10483): (1) no such column: Description
12-03 14:54:39.486: D/AndroidRuntime(10483): Shutting down VM
12-03 14:54:39.486: W/dalvikvm(10483): threadid=1: thread exiting with uncaught exception (group=0xa6192908)
12-03 14:54:39.486: E/AndroidRuntime(10483): FATAL EXCEPTION: main
12-03 14:54:39.486: E/AndroidRuntime(10483): java.lang.RuntimeException: Unable to start activity ComponentInfo{eite.dit.ulpgc.matteo.video/eite.dit.ulpgc.matteo.video.MainActivity}: android.database.sqlite.SQLiteException: no such column: Description (code 1): , while compiling: SELECT _id, Title, Description, Address FROM Video
12-03 14:54:39.486: E/AndroidRuntime(10483): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-03 14:54:39.486: E/AndroidRuntime(10483): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-03 14:54:39.486: E/AndroidRuntime(10483): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-03 14:54:39.486: E/AndroidRuntime(10483): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-03 14:54:39.486: E/AndroidRuntime(10483): at android.os.Handler.dispatchMessage(Handler.java:99)
12-03 14:54:39.486: E/AndroidRuntime(10483): at android.os.Looper.loop(Looper.java:137)
12-03 14:54:39.486: E/AndroidRuntime(10483): at android.app.ActivityThread.main(ActivityThread.java:5041)
12-03 14:54:39.486: E/AndroidRuntime(10483): at java.lang.reflect.Method.invokeNative(Native Method)
12-03 14:54:39.486: E/AndroidRuntime(10483): at java.lang.reflect.Method.invoke(Method.java:511)
12-03 14:54:39.486: E/AndroidRuntime(10483): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-03 14:54:39.486: E/AndroidRuntime(10483): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-03 14:54:39.486: E/AndroidRuntime(10483): at dalvik.system.NativeStart.main(Native Method)
12-03 14:54:39.486: E/AndroidRuntime(10483): Caused by: android.database.sqlite.SQLiteException: no such column: Description (code 1): , while compiling: SELECT _id, Title, Description, Address FROM Video
12-03 14:54:39.486: E/AndroidRuntime(10483): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
12-03 14:54:39.486: E/AndroidRuntime(10483): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
12-03 14:54:39.486: E/AndroidRuntime(10483): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
12-03 14:54:39.486: E/AndroidRuntime(10483): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
12-03 14:54:39.486: E/AndroidRuntime(10483): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
12-03 14:54:39.486: E/AndroidRuntime(10483): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
12-03 14:54:39.486: E/AndroidRuntime(10483): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
12-03 14:54:39.486: E/AndroidRuntime(10483): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
12-03 14:54:39.486: E/AndroidRuntime(10483): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
12-03 14:54:39.486: E/AndroidRuntime(10483): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
12-03 14:54:39.486: E/AndroidRuntime(10483): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
12-03 14:54:39.486: E/AndroidRuntime(10483): at eite.dit.ulpgc.matteo.video.dbdatasource.getAllData(dbdatasource.java:56)
12-03 14:54:39.486: E/AndroidRuntime(10483): at eite.dit.ulpgc.matteo.video.MainActivity.dbread(MainActivity.java:85)
12-03 14:54:39.486: E/AndroidRuntime(10483): at eite.dit.ulpgc.matteo.video.MainActivity.onCreate(MainActivity.java:43)
12-03 14:54:39.486: E/AndroidRuntime(10483): at android.app.Activity.performCreate(Activity.java:5104)
12-03 14:54:39.486: E/AndroidRuntime(10483): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-03 14:54:39.486: E/AndroidRuntime(10483): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-03 14:54:39.486: E/AndroidRuntime(10483): ... 11 more
这是我第一次在android上处理db。我检查了代码的各个时间,我无法得到错误的位置。
提前谢谢!