将记录插入sqlite db时强制关闭错误

时间:2013-03-28 15:39:54

标签: android sqlite

我正在开发我的第一个用户必须登录的应用程序。我正在使用SQLite数据库来存储用户名和密码。但是当我尝试向数据库添加新联系人时,我收到强制关闭错误。请帮助我哪里错了。以下是代码。

User.java文件主要用于getter和setter:

package com.example.first_db_app;

public class User 
{
    //private variables


int _userid;
String _uname;
String _upwd;

//empty constructor
public User()
{

}

//constructor
/*public User(int userid,String uname,String upwd)
{
    this._userid=userid;
    this._uname=uname;
    this._upwd=upwd;
}*/

//constructor
public User(String uname,String upwd)
{
    this._uname=uname;
    this._upwd=upwd;
}




//getting userid
public int getUserId()
{
    return this._userid;
}

//setting userid

public void setUserId(int userid )
{
    this._userid=userid;
}




//getting username

public String getUname()
{
    return this._uname;
}

//setting username
public void setUname(String uname)
{
    this._uname=uname;
}



//getting user password
public String getUpwd()
{
    return this._upwd;
}

//setting user password
public void setUpwd(String upwd)
{
    this._upwd=upwd;
}

}

我在其中创建了数据库和CRUD操作的DatabaseHandler.java

package com.example.first_db_app;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

//creating database
public class DatabaseHandler extends SQLiteOpenHelper
{


//private static variables

private static final int DATABASE_VERSION=1;

//database name
private static final String DATABASE_NAME="UserDB";

//table name
private static final String TABLE_NAME="Users";

//table column names
private static final String USER_ID="id";
private static final String USER_NAME="u_name";
private static final String USER_PWD="u_pwd";

public DatabaseHandler(Context context)
{
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    // TODO Auto-generated constructor stub
}


//creating tables
@Override
public void onCreate(SQLiteDatabase db)
{
    // TODO Auto-generated method stub
    String CREATE_USERS_TABLE= "CREATE TABLE"+ TABLE_NAME+ "("+  USER_NAME +      "TEXT,"+ USER_PWD + "TEXT" + ")";
    db.execSQL(CREATE_USERS_TABLE);
}




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

    //drop older table if existed

    db.execSQL("DROP TABLE IF EXISTS"+ TABLE_NAME);
    //creates table again
    onCreate(db);

}


//adding new users to the database
public void addUser(User user)
{

    SQLiteDatabase db=this.getWritableDatabase();

    ContentValues values=new ContentValues();
    //values.put(USER_ID, user.getUserId());
    values.put(USER_NAME, user.getUname());
    values.put(USER_PWD, user.getUpwd());

    //inserting row
    db.insert(TABLE_NAME, null, values);
    db.close();

}



//reading a single row.this is done by using cursor

public String getUser(String  Uname)
{
    SQLiteDatabase db=this.getReadableDatabase();
    Cursor cursor=db.query(TABLE_NAME,new String[] {USER_NAME,USER_PWD},USER_NAME + "=?",new String[]{String.valueOf(Uname)},null,null,null,null);

    if (cursor!=null)
        cursor.moveToFirst();

    //User user=new User(cursor.getString(0),cursor.getString(1));
    String password=cursor.getString(cursor.getColumnIndex(USER_PWD));  
    cursor.close();
    //return user
    return password;
}


//Updating a  single record in database

public int updateUser(User user)
{
    SQLiteDatabase db=this.getWritableDatabase();

    ContentValues values=new ContentValues();
    values.put(USER_NAME,user.getUname());
    values.put(USER_PWD, user.getUpwd());

    //updating row
    db.update(TABLE_NAME, values, USER_NAME + "=?" ,new String[]{String.valueOf(user.getUname())});
    return 0;

}

//Deleting a single record in database
public void deleteUser(User user)
{
    SQLiteDatabase db=this.getWritableDatabase();

    db.delete(TABLE_NAME, USER_NAME + "=?", new String[]{String.valueOf(user.getUname())});
    db.close();
}

}

我使用以下文件进行用户登录,其中包含用户名和密码的文本框以及一个按钮

TestDbActivity.java

package com.example.first_db_app;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class TestDbActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_db);

    DatabaseHandler db=new DatabaseHandler(this);


    //CRUD operations

    //inserting the records
    Log.d("Insert: ", "Inserting ..");
    db.addUser(new User("Ashwin","11111"));
    db.addUser(new User("Ravi","22222"));
    db.addUser(new User("Gopal","33333"));
    db.addUser(new User("Satish","44444"));

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.test_db, menu);
    return true;
}

}

1 个答案:

答案 0 :(得分:1)

我怀疑创建数据库时出错。

之间没有空格
  1. “创建表格”和TABLE_NAME的值
  2. USER_NAME的价值和“TEXT”
  3. USER_PWD的价值和“TEXT”
  4. 插入新记录似乎很好。

    String CREATE_USERS_TABLE = String.format("CREATE TABLE %s (%s TEXT, %s TEXT)", TABLE_NAME, USER_NAME, USER_PWD);
    db.execSQL(CREATE_USERS_TABLE);
    

    所以要小心你的陈述