这是我的 dbHelper 类的代码:
package com.example.emp_management;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, dbName, null, 1);
// TODO Auto-generated constructor stub
}
static final String dbName="EmployeeManagementSystem";
static final String Login_Table="Login_Authentication";
static final String colID="ID";
static final String colUsername="Username";
static final String colPassword="Passwrod";
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
//db.execSQL("CREATE TABLE" + Login_Table+ "(" +
// colID + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
// colUsername + "TEXT NOT NULL," +
// colPassword + "TEXT NOT NULL);"
//);
//db.execSQL("CREATE TABLE "+Login_Table+" ("+colID+ " INTEGER PRIMARY KEY AUTOINCREMENT ,"+
// colUsername+ " TEXT ,"+ colPassword + "TEXT");
db.execSQL("CREATE TABLE " + Login_Table + "(colID INTEGER PRIMARY KEY AUTOINCREMENT, colUsername TEXT NOT NULL,colPassword TEXT NOT NULL);");
Log.w("Come aww man", "Database Table Created!!");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + "Login_Authentication");
onCreate(db);
}
public void insert_new_user(String username,String password)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(DatabaseHelper.colUsername,username);
cv.put(DatabaseHelper.colPassword,password);
db.insert(Login_Table, null, cv);
db.close();
}
}
这是用于将新员工添加到我创建的表中的代码:
package com.example.emp_management;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Adding_Employee extends Activity{
@Override
protected void onCreate(Bundle aglakaam) {
// TODO Auto-generated method stub
super.onCreate(aglakaam);
setContentView(R.layout.add_employee);
final EditText new_user = (EditText) findViewById(R.id.editText1);
final EditText new_pass = (EditText) findViewById(R.id.editText2);
final Button create_acc = (Button) findViewById(R.id.creat_acc);
create_acc.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DatabaseHelper accessing_db = new DatabaseHelper(Adding_Employee.this);
accessing_db.insert_new_user(new_user.getText().toString(), new_pass.getText().toString());
Toast.makeText(getApplicationContext(), "New User Has Been Created!!", Toast.LENGTH_SHORT).show();
}
});
}
}
这是我的日志:
02-20 02:42:26.491: E/Database(384): Error inserting Passwrod=1234 Username=sjdf
02-20 02:42:26.491: E/Database(384): android.database.sqlite.SQLiteException: table Login_Authentication has no column named Passwrod: , while compiling: INSERT INTO Login_Authentication(Passwrod, Username) VALUES(?, ?);
02-20 02:42:26.491: E/Database(384): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
对我来说有点奇怪的是用户名列存在。为什么它只显示密码而不是用户名的异常,即使存在密码列,为什么这里会抛出异常。请帮助我!