0

我是安卓新手。第一次将数据插入数据库。

数据库建表和插入数据的代码如下:

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);");

但它给了我同样的错误。

4

2 回答 2

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 );

于 2013-08-17T11:20:27.910 回答
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);
}

只需像这样创建数据库和表,它将成功运行......正如我正在尝试的那样。只是在清单文件中授予权限......对于 Access_EXTERNAL_STORAGE

于 2013-08-17T11:21:31.197 回答