-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,

AUTOINCREMENTNOT NULL暗示されています。必要に応じて追加できますが、それらは「シンタックス シュガー」です。

于 2012-12-18T22:02:58.237 に答える
1

あなたのコードを実行しようとしました。

問題は、データベースの作成ourDatabase.execSQL(DATABASE_CREATE); が失敗することです。

原因: android.database.sqlite.SQLiteException: AUTOINCREMENT は、コンパイル中に INTEGER PRIMARY KEY: でのみ許可されます:

INT(11) ではなく、INTEGER PRIMARY KEY として id を宣言する必要があります。

テーブルの作成に失敗すると、その後の挿入は機能しません。

于 2012-12-18T22:02:10.073 に答える