我是安卓新手。第一次将数据插入数据库。
数据库建表和插入数据的代码如下:
package com.example.hello_world;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class HNT {
public static final String KEY_ROWID="_id";
public static final String KEY_FName="person_Fname";
public static final String KEY_LName="person_Lname";
private static final String Database_Name="DBPersonInfo";
private static final String Database_Table="DBTable";
private static final int Database_Version=1;
private DBHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDB;
private static class DBHelper extends SQLiteOpenHelper{
public DBHelper(Context context) {
super(context, Database_Name, null, Database_Version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL( "create table "+Database_Table+ "("+
KEY_ROWID+"integer primary key autoincrement," +
KEY_FName+"TEXT NOT NULL, "+
KEY_LName+"TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int arg2) {
// TODO Auto-generated method stub
db.execSQL("drop table if exists"+ Database_Table );
onCreate(db);
}
}
public HNT (Context c)
{
ourContext=c;
}
public HNT open() throws SQLException
{
ourHelper=new DBHelper(ourContext);
ourDB=ourHelper.getWritableDatabase();
return this;
}
public void close()
{
ourHelper.close();
}
public long createEntry(String fName, String lName) {
// TODO Auto-generated method stub
ContentValues cv=new ContentValues();
cv.put(KEY_FName, fName);
cv.put(KEY_LName, lName);
return ourDB.insert(Database_Table, null, cv);
}
public String getData() {
// TODO Auto-generated method stub
String[] columns=new String[]{KEY_ROWID,KEY_FName,KEY_LName};
Cursor c=ourDB.query(Database_Table, columns, null, null, null, null, null);
String result="";
int iRow=c.getColumnIndex(KEY_ROWID);
int iFName=c.getColumnIndex(KEY_FName);
int iLName=c.getColumnIndex(KEY_LName);
for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
{
result=result+c.getString(iRow)+ " " + c.getString(iFName)+ " " + c.getString(iLName)+ "\n";
}
return result;
}
}
它在 logcat 上向我显示不存在 person_Fname 列的错误。
Logcat如下:
请帮我。
它不允许我插入数据。(基本上是表格形成错误)。
我的表格形成或插入的语法或方法有问题吗?
编辑:
我编辑了表格创建部分,其中包含空格:
db.execSQL( "create table "+Database_Table+ "("+
KEY_ROWID+" integer primary key autoincrement," +
KEY_FName+" TEXT NOT NULL, "+
KEY_LName+" TEXT NOT NULL);");
但它给了我同样的错误。