-1

当我尝试将一些数据插入到我在 android 中创建的 SQLliteDatabase 时遇到问题。

下面是我在名为 SQLlitenew 的类中创建数据库的代码:

public class SQLlitenew{
    public static final String KEY_ROWID="id";
    public static final String KEY_PRODUCTNAME="ProductName";
    public static final String KEY_PRODUCTTYPE="ProductType";
    public static final String KEY_LOCATION="place";
    public static final String KEY_WORKER="worker";
    public static final String KEY_QUESTION1="Question1";
    public static final String KEY_QUESTION2="Question2";
    public static final String KEY_QUESTION3="Question3";
    public static final String KEY_QUESTION4="Question4";
    public static final String KEY_QUESTION5="Question5";
    public static final String KEY_TIME="time";
    public static final String KEY_UPLOADED="uploaded"; 
    public static final String KEY_EMAIL="emailAdd";
    public static final String TAG="DBAdapter"; // This is for logging information e.g LOG.d

    private static final String DATABASE_NAME="NOID";
    private final static String DATABASE_TABLE="session";

    private static final int DATABASE_VERSION = 2;


    private static final String DATABASE_CREATE = "create table if not exists " + DATABASE_TABLE + " (id INT(11) primary key autoincrement, " +
            "ProductName VARCHAR, ProductType VARCHAR, place INT(5), worker VARCHAR, " +
            "Question1 VARCHAR, Question2 VARCHAR, Question3 VARCHAR, Question4 VARCHAR, Question5 VARCHAR, " +
            "time long, uploaded INT(1), emailAdd VARCHAR );";


    //create instant of DBhelper class
    private DBHelper ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDatabase;

    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 ourDatabase) {
            // TODO Auto-generated method stub

            //First create the database table
            try{
            ourDatabase.execSQL(DATABASE_CREATE);
            Log.w(DATABASE_CREATE,"successssssssssssssss!");

            }catch(SQLException e){
                e.printStackTrace();
                Log.w(DATABASE_CREATE,"FFFAAAAIIIIIILLLLLLEEEDDDDDDDDDDDD!");
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            //----always move the Data before Deleting the Data----->
            Log.w(TAG, "This is will delete the previous data which will make you lose all information from the" +oldVersion + "and will create a new database to the " + newVersion);
            db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
            onCreate(db);
        }

    }

    //set up public contructor
    public SQLlitenew(Context c){
        this.ourContext = c;
        ourHelper = new DBHelper(c);

    }

    public long insertRecord(String productName,  String productType, String place, String name, String ans1, String ans2, String ans3, String firstChoice, String secondChoice, long time, int uploaded, String email){

        //writing stuff into db
        ContentValues content = new ContentValues();

        content.put(KEY_PRODUCTNAME, productName);
        content.put(KEY_PRODUCTTYPE, productType);
        content.put(KEY_LOCATION, place);
        content.put(KEY_WORKER, name);
        content.put( KEY_QUESTION1, ans1);
        content.put( KEY_QUESTION2, ans2);
        content.put( KEY_QUESTION3, ans3);
        content.put( KEY_QUESTION4, firstChoice);
        content.put( KEY_QUESTION5, secondChoice);
        content.put(KEY_TIME, time);
        content.put( KEY_UPLOADED, uploaded);
        content.put(KEY_EMAIL, email);      


        return ourDatabase.insert(DATABASE_TABLE, null, content);

    }
    public void close(){
        ourHelper.close();

    }

    public SQLiteDatabase getWritableDatabase() {
        ourHelper = new DBHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        // TODO Auto-generated method stub
        return null;
    }
}

这是一个单独的类中的代码,我在其中调用函数来插入数据:

SQLlitenew entry = new SQLlitenew(this);
entry.open();
entry.insertRecord(product, productType, place, name, ans1, ans2, ans3, firstChoice, secondChoice, time, uploaded, email);
entry.close();

问题是当我尝试插入时,它会带来这个错误:

12-18 13:08:45.029: I/Database(4282): sqlite returned: error code = 1, msg = no such table: session
12-18 13:08:45.044: E/Database(4282): Error inserting time=1355864924948 ProductType=Food Question5=Fresh & Clean Scents i.e nourishing milk & honey that hydrates and protects
12-18 13:08:45.044: E/Database(4282):  uploaded=0 Question4=Chip resistant nail polish 
12-18 13:08:45.044: E/Database(4282):  worker=ttt Question3=YES ProductName=tyyyy Question2=African American Question1=Under 25 place=56677 emailAdd=t@n.com
12-18 13:08:45.044: E/Database(4282): android.database.sqlite.SQLiteException: no such table: session: , while compiling: INSERT INTO session(time, ProductType, Question5, uploaded, Question4, worker, Question3, ProductName, Question2, Question1, place, emailAdd) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);

将 database_version 提升到 3 的新错误。

12-18 14:00:38.794: I/Database(4384): sqlite returned: error code = 1, msg = AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY
12-18 14:00:38.794: E/Database(4384): Failure 1 (AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY) on 0x279960 when preparing 'create table if not exists session (id INT(11) primary key autoincrement, ProductName VARCHAR, ProductType VARCHAR, place INT(5), worker VARCHAR, Question1 VARCHAR, Question2 VARCHAR, Question3 VARCHAR, Question4 VARCHAR, Question5 VARCHAR, time long, uploaded INT(1), emailAdd VARCHAR );'.
12-18 14:00:38.802: W/System.err(4384): android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY: create table if not exists session (id INT(11) primary key autoincrement, ProductName VARCHAR, ProductType VARCHAR, place INT(5), worker VARCHAR, Question1 VARCHAR, Question2 VARCHAR, Question3 VARCHAR, Question4 VARCHAR, Question5 VARCHAR, time long, uploaded INT(1), emailAdd VARCHAR );

我想不通,它一直让我发疯!感谢您花时间研究这个问题。

4

2 回答 2

2

对于 Android,您INTEGER PRIMARY KEY应该命名_id并根据您需要使用的错误INTEGERnot INT(11)。正确的语法是:

_id INTEGER PRIMARY KEY,

AUTOINCREMENT并且NOT NULL是隐含的。您可以根据需要添加它们,但它们是“语法糖”。

于 2012-12-18T22:02:58.237 回答
1

我试图执行你的代码。

问题是数据库创建ourDatabase.execSQL(DATABASE_CREATE); 失败是因为

引起:android.database.sqlite.SQLiteException: AUTOINCREMENT 只允许在 INTEGER PRIMARY KEY: 上,同时编译:

您需要将 id 声明为 INTEGER PRIMARY KEY,而不是 INT(11)

鉴于表创建失败,后续插入将不起作用。

于 2012-12-18T22:02:10.073 回答