将值插入sqlite数据库

时间:2014-12-08 19:08:17

标签: android

在注册函数返回-1

上插入函数
private String customerTable="create table customer (id integer primary key autoincrement, custname text not null,username text not null,password text notnull" +
        "gender text ,birthdate text,jop text not null); ";
public long Registration (String name,String username,String password,String Gender,String birthdate,String jop)
{
    Ecommerce=getWritableDatabase();
    ContentValues row=new ContentValues();
    //row.put("id",null);
    row.put("custname", name);
    row.put("username",username );
    row.put("password", password);
    row.put("gender", Gender);
    row.put("birthdate", birthdate);
    row.put("jop", jop);
 return Ecommerce.insert("customer", null, row);
}
主要活动中的

dbobject.Registration(customer_name, user_name, passwordfirst,customer_gender , cus_birthdate, customer_jop);

logcat的

12-08 19:35:40.788: D/dalvikvm(1608): GC_CONCURRENT freed 63K, 7% free 2819K/3008K, paused 82ms+89ms, total 298ms
12-08 19:35:41.219: D/gralloc_goldfish(1608): Emulator without GPU emulation detected.
12-08 19:35:48.188: I/Choreographer(1608): Skipped 54 frames!  The application may be doing too much work on its main thread.
12-08 19:35:59.458: D/dalvikvm(1608): GC_CONCURRENT freed 300K, 13% free 2912K/3336K, paused 84ms+105ms, total 285ms
12-08 19:36:02.778: E/SQLiteLog(1608): (1) table customer has no column named gender
12-08 19:36:02.828: E/SQLiteDatabase(1608): Error inserting jop=sa birthdate=23 gender=Female username=aya password=123 custname=aya
12-08 19:36:02.828: E/SQLiteDatabase(1608): android.database.sqlite.SQLiteException: table customer has no column named gender (code 1): , while compiling: INSERT INTO customer(jop,birthdate,gender,username,password,custname) VALUES (?,?,?,?,?,?)
12-08 19:36:02.828: E/SQLiteDatabase(1608):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
12-08 19:36:02.828: E/SQLiteDatabase(1608):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
12-08 19:36:02.828: E/SQLiteDatabase(1608):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
12-08 19:36:02.828: E/SQLiteDatabase(1608):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
12-08 19:36:02.828: E/SQLiteDatabase(1608):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
12-08 19:36:02.828: E/SQLiteDatabase(1608):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
12-08 19:36:02.828: E/SQLiteDatabase(1608):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
12-08 19:36:02.828: E/SQLiteDatabase(1608):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
12-08 19:36:02.828: E/SQLiteDatabase(1608):     at com.example.ecommercepro.TablesDataBase.Registration(TablesDataBase.java:41)
12-08 19:36:02.828: E/SQLiteDatabase(1608):     at com.example.ecommercepro.MainActivity$1.onClick(MainActivity.java:72)
12-08 19:36:02.828: E/SQLiteDatabase(1608):     at android.view.View.performClick(View.java:4204)
12-08 19:36:02.828: E/SQLiteDatabase(1608):     at android.view.View$PerformClick.run(View.java:17355)
12-08 19:36:02.828: E/SQLiteDatabase(1608):     at android.os.Handler.handleCallback(Handler.java:725)
12-08 19:36:02.828: E/SQLiteDatabase(1608):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-08 19:36:02.828: E/SQLiteDatabase(1608):     at android.os.Looper.loop(Looper.java:137)
12-08 19:36:02.828: E/SQLiteDatabase(1608):     at android.app.ActivityThread.main(ActivityThread.java:5041)
12-08 19:36:02.828: E/SQLiteDatabase(1608):     at java.lang.reflect.Method.invokeNative(Native Method)
12-08 19:36:02.828: E/SQLiteDatabase(1608):     at java.lang.reflect.Method.invoke(Method.java:511)
12-08 19:36:02.828: E/SQLiteDatabase(1608):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-08 19:36:02.828: E/SQLiteDatabase(1608):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-08 19:36:02.828: E/SQLiteDatabase(1608):     at dalvik.system.NativeStart.main(Native Method)

public class TablesDataBase extends SQLiteOpenHelper {
private static String dbname="Ecommerce";
private String customerTable="create table customer (id integer primary key autoincrement, custname text not null,username text not null,password text notnull ,gender text,birthdate text,jop text not null); ";
// private String OrderTable="create table order (order_id integer primary key  autoincrement,cust_id integer not null,FOREIGN KEY (cust_id) REFERENCES customer(id)";
private SQLiteDatabase Ecommerce;

public TablesDataBase(Context context) {
    super(context,dbname, null, 2);
}
@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(customerTable);
    //db.execSQL(OrderTable);

}
public long Registration (String name,String username,String password,String Gender,String birthdate,String jop)
{
    Ecommerce=getWritableDatabase();
     try {
ContentValues row=new ContentValues();
    //row.put("id",null);
    row.put("custname", name);
    row.put("username",username );
    row.put("password", password);
    row.put("gender", Gender);
    row.put("birthdate", birthdate);
    row.put("jop", jop);
 return Ecommerce.insert("customer", null, row);
     }
     catch (Exception e)
     {
return 10;
     }
//  Ecommerce.execSQL("INSERT INTO customer VALUES('"+null +"','"+name+"','"+username+"','"+password+"','"+Gender+"','"+birthdate+",'"+ jop+"');");
}
public Cursor checkauth(String username,String password)
{
    Ecommerce=getReadableDatabase();
//  Cursor curser =Ecommerce.rawQuery("select * from customer where username = ? and password =?", new String []{username,password});
    Cursor curser =Ecommerce.rawQuery("select * from customer where username =? and password =?",new String []{username,password});
    curser.moveToFirst();
    return curser;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}

}

1 个答案:

答案 0 :(得分:0)

,之后,您的SQL查询()中错过了notnull [{1}}和customerTable之间的空格} 。使用此查询并更改数据库的版本:

password text notnull

然后将表格放入private String customerTable = "CREATE TABLE IF NOT EXISTS customer (" + "id integer primary key autoincrement," + "custname text not null," + "username text not null," + "password text not null," + "gender text," + "birthdate text," + "jop text not null); "; 方法:

onUpgrade