2013-03-28 49 views
0

我正在開發我的第一個應用程序,其中用戶必須登錄。我使用SQLite數據庫來存儲用戶名和密碼。但我越來越強制關閉錯誤,當我我試圖添加新的聯繫人到數據庫。請幫我在哪裏我錯了。以下是代碼。強制關閉錯誤來插入記錄到sqlite db

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

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; 
} 

}

在我所創建的DB和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(); 
} 

}

和IM使用用戶登錄到以下文件中包含的文本框中輸入用戶名和密碼,一鍵

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; 
} 

}

+2

什麼異常堆棧? – waqaslam

回答

1

我懷疑上創建一個錯誤的數據庫。有之間沒有空格

  1. 「CREATE TABLE」 和TABLE_NAME的價值
  2. USER_NAME的價值和 「文本」
  3. USER_PWD的價值和 「文本」

插入新記錄看起來不錯。

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

所以,要小心你的發言

+0

請花一點時間來描述你修復的問題和原因,否則很難說「CREATE_USERS_TABLE」是真正的問題。 – Sam

+0

是的,我錯過了空間。非常感謝..它的工作原理。你搖滾。 – user2179989