无法在SQLite数据库中创建表和列

时间:2015-05-04 16:40:38

标签: android mysql database sqlite

我为我的应用程序创建了数据库,并为表创建生成了代码。它产生如下错误。

数据库编码(DatabaseHandler3)

 import java.util.ArrayList;
 import java.util.List;
 import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class DatabaseHandler3 extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "DOCROID.sqlite";
private static final String TABLE_CONTACTS = "UserRegistration";
private static final String username = "name";
private static final String userage = "age";
private static final String usergender = "gender";
private static final String useraddress = "address";
private static final String usercode = "code";
private static final String userid = "id";
private static final String userpassword = "password";
private static final String usercpass = "cpass";
private static final String LOGCAT = null;
private static Context context;
public DatabaseHandler3(Context context) {
    super(context, "/sdcard/DOCROID/" + DATABASE_NAME, null, DATABASE_VERSION);
    Log.d("DatabaseHandler3","Database Created");
    // TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

    db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "(" + username + " TEXT" + userage + " INTEGER " + usergender + " TEXT" + useraddress + " TEXT" + usercode + " INTEGER" + userid + " INTEGER" + userpassword + " BLOB" + usercpass + " BLOB ) ;");
    Log.d("DatabaseHandler3", " User Table Created");


}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}
public void addContact(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(username, contact.getNAME());
    values.put(userage, contact.getAGE());
    values.put(usergender, contact.getGENDER());
    values.put(useraddress, contact.getADDRESS());
    values.put(usercode, contact.getCODE());
    values.put(userid, contact.getID());
    values.put(userpassword, contact.getPASSWORD());
    values.put(usercpass, contact.getCPASS());
   // db.insert(TABLE_CONTACTS, null, values);
    db.execSQL("insert into UserRegistration (username,userage,usergender,useraddress,usercode,userid,userpass,usercpass) values ('" + username +"','"+ userage + "','" + usergender +"','" + useraddress + "','" + usercode +"','" + userid + "','" + userpassword + "','" + usercpass + "')");
    db.close();
}
public List<Contact> getAllContacts() {
    List<Contact> contactList = new ArrayList<Contact>();
    String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            Contact contact = new Contact();
            contact.setNAME(cursor.getString(0));
            contact.setAGE(cursor.getString(1));
            contact.setGENDER(cursor.getString(2));
            contact.setADDRESS(cursor.getString(3));
            contact.setCODE(cursor.getString(4));
            contact.setID(cursor.getString(5));
            contact.setPASSWORD(cursor.getString(6));
            contact.setCPASS(cursor.getString(7));
            contactList.add(contact);
        } while (cursor.moveToNext());
    }
    return contactList;
}
public void deleteall() {
    SQLiteDatabase db = this.getWritableDatabase();
    String selectQuery = "DELETE FROM " + TABLE_CONTACTS;
    db.execSQL(selectQuery);
}
}

主要代码(userregistration.java)

 public class userregistration extends ActionBarActivity {
 EditText UserName,UserAge,UserGender,Useraddress,Userpincode, Userid,UserPass,UserCPass;
 String username,userage, usergender, useraddress, usercode, userid, userpass,usercpass;
public SQLiteDatabase db = null;
Button Reg;
Context ctx = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_userregistration);
    UserName = (EditText) findViewById(R.id.editText11);
    UserAge = (EditText) findViewById(R.id.editText69);
    UserGender = (EditText) findViewById(R.id.editText70);
    Useraddress = (EditText) findViewById(R.id.editText73);
    Userpincode = (EditText) findViewById(R.id.editText74);
    Userid = (EditText) findViewById(R.id.editText75);
    UserPass = (EditText) findViewById(R.id.editText76);
    UserCPass = (EditText) findViewById(R.id.editText72);
    Reg = (Button) findViewById(R.id.button24);
    Reg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            username =UserName.getText().toString();
            userage = UserAge.getText().toString();
            usergender = UserGender.getText().toString();
            useraddress = Useraddress.getText().toString();
            usercode = Userpincode.getText().toString();
            userid = Userid.getText().toString();
            userpass = UserPass.getText().toString();
            usercpass = UserCPass.getText().toString();

            if (username.trim().equals("")&& userage.trim().equals("") && usergender.trim().equals("") && useraddress.trim().equals("") && usercode.trim().equals("") && userid.trim().equals("") && userpass.trim().equals("") && usercpass.trim().equals(""))
            {
                Toast.makeText(getBaseContext(),"Fields should not empty",Toast.LENGTH_LONG).show();
            }
            else
            {
              DatabaseHandler3 db=new DatabaseHandler3(userregistration.this);
                db.addContact(new Contact(username, userage, usergender, useraddress, usercode, userid, userpass, usercpass));
                Toast.makeText(getBaseContext(),"Registration Success",Toast.LENGTH_LONG).show();
             finish();
            }
        }
    }) ;
}

错误报告:

  05-04 21:49:20.516    6128-6128/com.example.bathrivasudevan.docroid     E/SQLiteLog﹕ (1) table UserRegistration has no column named username
  05-04 21:49:20.556    6128-6128/com.example.bathrivasudevan.docroid E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.database.sqlite.SQLiteException: table UserRegistration has no column named username (code 1): , while compiling: insert into UserRegistration (username,userage,usergender,useraddress,usercode,userid,userpass,usercpass) values ('name','age','gender','address','code','id','password','cpass')
        at     android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1108)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:681)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:589)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1769)
        at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1700)
        at com.example.bathrivasudevan.docroid.DatabaseHandler3.addContact(DatabaseHandler3.java:60)
        at com.example.bathrivasudevan.docroid.userregistration$1.onClick(userregistration.java:85)
        at android.view.View.performClick(View.java:4275)
        at android.view.View$PerformClick.run(View.java:17434)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:177)
        at android.app.ActivityThread.main(ActivityThread.java:4947)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
        at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

列&#34;用户名&#34;如果不存在,您创建了一个名为&#34; name&#34;的列。查看您的用户名变量

private static final String username = "name";

您正在引用名称为&#34; username&#34;

的列
 db.execSQL("insert into UserRegistration (username,userage,usergender,useraddress,usercode,userid,userpass,usercpass) values ('" + username +"','"+ userage + "','" + usergender +"','" + useraddress + "','" + usercode +"','" + userid + "','" + userpassword + "','" + usercpass + "')");

更改其中一个字符串,使它们相等。

编辑:您似乎有几个这样的问题,解决所有问题。

答案 1 :(得分:0)

使用像这样添加,在类型

之后
db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "(" + username + " TEXT," + userage + " INTEGER, " + usergender + " TEXT," + useraddress + " TEXT," + usercode + " INTEGER," + userid + " INTEGER," + userpassword + " BLOB," + usercpass + " BLOB)");