我是android新手。首次将数据插入数据库。
数据库表格形成和数据插入的代码如下:
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);");
但它给了我同样的错误。
答案 0 :(得分:1)
您在数据库创建中缺少空格。变化:
db.execSQL( "create table "+Database_Table+ "("+
KEY_ROWID+"integer primary key autoincrement," +
KEY_FName+"TEXT NOT NULL, "+
KEY_LName+"TEXT NOT NULL);"
到
db.execSQL( "create table "+Database_Table+ "("+
KEY_ROWID+" integer primary key autoincrement," +
KEY_FName+" TEXT NOT NULL, "+
KEY_LName+" TEXT NOT NULL);"
此外:
db.execSQL("drop table if exists"+ Database_Table );
为:
db.execSQL("drop table if exists "+ Database_Table );
答案 1 :(得分:1)
公共类DataBaseHelper扩展了SQLiteOpenHelper {
Context mContext;
private static final String DATABASE_NAME = "Personal_Assistant_Chronical.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_DETAIL = "newContact";
private static final String COLUMN_PERSON_ID= "personID";
private static final String COLUMN_PERSON_NAME= "person_name";
private static final String COLUMN_MOBILE_NUMBER = "mobile_no";
private static final String COLUMN_LANDLINE_NUMBER = "lanline_no";
private static final String COLUMN_FAX_NUMBER = "fax_no";
private static final String COLUMN_EMAIL_ID = "email_id";
private static final String COLUMN_PERSONAL_ADDRESS = "personal_address";
private static final String COLUMN_WORK_ADDRESS = "work_address";
private static final String CREATE_DETAIL_TABLE = "create table if not exists "
+ TABLE_DETAIL
+ "("
+ COLUMN_PERSON_ID
+" INTEGER primary key autoincrement, "
+ COLUMN_PERSON_NAME
+ " VARCHAR(100) NOT NULL UNIQUE, "
+ COLUMN_MOBILE_NUMBER
+ " VARCHAR(100) NOT NULL , "
+ COLUMN_LANDLINE_NUMBER
+ " VARCHAR(100) NOT NULL, "
+ COLUMN_FAX_NUMBER
+ " VARCHAR(100) NOT NULL, "
+ COLUMN_EMAIL_ID
+ " VARCHAR(100) NOT NULL, "
+ COLUMN_PERSONAL_ADDRESS
+ " VARCHAR(100) NOT NULL, "
+ COLUMN_WORK_ADDRESS
+ " VARCHAR(100) NOT NULL); "
;
private static final String TABLE_PERSONAL = "newPersonal";
private static final String COLUMN_DATE_ID= "dateID";
private static final String COLUMN_DATE= "fate";
private static final String COLUMN_DIARY= "diary";
private static final String CREATE_PERSONAL_TABLE = "create table if not exists "
+ TABLE_PERSONAL
+ "("
+ COLUMN_DATE_ID
+" INTEGER primary key autoincrement , "
+ COLUMN_DATE
+" VARCHAR(100) NOT NULL UNIQUE, "
+ COLUMN_DIARY
+ " VARCHAR(100) NOT NULL); "
;
public DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.mContext=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DETAIL_TABLE);
db.execSQL(CREATE_PERSONAL_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (db != null)
onCreate(db);
}
只需像这样创建数据库和表它就会成功运行.....我正在尝试。只是在清单文件中给予权限... for Access_EXTERNAL_STORAGE