0

这是我的 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)

对我来说有点奇怪的是用户名列存在。为什么它只显示密码而不是用户名的异常,即使存在密码列,为什么这里会抛出异常。请帮助我!

4

3 回答 3

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);");
于 2013-02-19T21:57:51.323 回答
1

也许你只需要 colID 、 colUsername 、 colPassword 之后的空格

db.execSQL("CREATE TABLE " + Login_Table + "(" + colID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + colUsername + " TEXT NOT NULL," + colPassword + " TEXT NOT NULL);");

于 2013-02-19T22:25:45.310 回答
-1

您将 Password 拼错为 Passwrod。简单的错字。此外,Java 不进行变量字符串插值,您需要使用连接。

于 2013-02-19T21:57:05.157 回答