这是我的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();
}
});
}
}
这是我的logcat:
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)
这里对我来说很奇怪的是用户名列存在。为什么它只显示密码的异常,而不是用户名,甚至当密码列存在时,为什么抛出异常。请帮助我!
答案 0 :(得分:1)
您正在创建名称为colPassword,colUsername和colID
的列db.execSQL("CREATE TABLE " + Login_Table + "(colID INTEGER PRIMARY KEY AUTOINCREMENT, colUsername TEXT NOT NULL,colPassword TEXT NOT NULL);");
然后尝试使用
访问它们static final String colID="ID";
static final String colUsername="Username";
static final String colPassword="Passwrod";
由于Passwrod!= colPassword ..当然你将无法找到该列。
尝试这样的操作,以确保在创建表格时访问列时使用相同的列名称。 您需要增加数据库版本才能显示更改。
db.execSQL("CREATE TABLE " + Login_Table + "(" + colID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + colUsername + " TEXT NOT NULL," + colPassword + " TEXT NOT NULL);");
答案 1 :(得分:1)
也许你只需要colID,colUsername,colPassword
之后的空格db.execSQL(“CREATE TABLE”+ Login_Table +“(”+ colID +“INTEGER PRIMARY KEY AUTOINCREMENT,”+ colUsername +“TEXT NOT NULL,”+ colPassword +“TEXT NOT NULL);”);
答案 2 :(得分:-1)
您拼错密码为密码。简单的拼写错误。另外,Java不进行变量字符串插值,需要使用连接。