android更新表中的行

时间:2016-06-19 15:24:24

标签: android mysql phpmyadmin

我有一个带有mysql数据库的wamp服务器: Database

我有一个登录和注册系统。我希望允许我的用户随时更改用户名。

在上图中,您将看到一个用户。我的目标是更改此用户的名称。

我有我的数据库课程:

public class SQLiteHandler extends SQLiteOpenHelper {

private static final String TAG = SQLiteHandler.class.getSimpleName();

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "android_api";

// Login table name
private static final String TABLE_USER = "user";

// Login Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_EMAIL = "email";
private static final String KEY_UID = "uid";
private static final String KEY_CREATED_AT = "created_at";

public SQLiteHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USER + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_EMAIL + " TEXT UNIQUE," + KEY_UID + " TEXT,"
            + KEY_CREATED_AT + " TEXT" + ")";
    db.execSQL(CREATE_LOGIN_TABLE);

    Log.d(TAG, "Database tables created");
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);

    // Create tables again
    onCreate(db);
}

/**
 * Storing user details in database
 * */
public void addUser(String name, String email, String uid, String created_at) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, name); // Name
    values.put(KEY_EMAIL, email); // Email
    values.put(KEY_UID, uid); // Email
    values.put(KEY_CREATED_AT, created_at); // Created At

    // Inserting Row

    long id = db.insert(TABLE_USER, null, values);
    db.close(); // Closing database connection

    Log.d(TAG, "New user inserted into sqlite: " + id);
}

/**
 * Getting user data from database
 * */
public HashMap<String, String> getUserDetails() {
    HashMap<String, String> user = new HashMap<String, String>();
    String selectQuery = "SELECT  * FROM " + TABLE_USER;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    // Move to first row
    cursor.moveToFirst();
    if (cursor.getCount() > 0) {
        user.put("name", cursor.getString(1));
        user.put("email", cursor.getString(2));
        user.put("uid", cursor.getString(3));
        user.put("created_at", cursor.getString(4));
    }
    cursor.close();
    db.close();
    // return user
    Log.d(TAG, "Fetching user from Sqlite: " + user.toString());

    return user;
}

/**
 * Re crate database Delete all tables and create them again
 * */
public void deleteUsers() {
    SQLiteDatabase db = this.getWritableDatabase();
    // Delete All Rows
    db.delete(TABLE_USER, null, null);
    db.close();


    Log.d(TAG, "Deleted all user info from sqlite");
}

}

除了所有这些课程,我还为升级添加了这个课程:

public void updateUsername(String uid, String name) {

    SQLiteDatabase db = this.getWritableDatabase();

    //Values i want to upgrade  
    ContentValues values = new ContentValues();
    values.put(KEY_NAME, name); // Name

    String whereQuery = "unique_id= " + uid;

    // Update Row
    db.update(TABLE_USER, values, whereQuery, null);
    db.close(); // Closing database connection

    Log.d(TAG, "Username updated into sqlite: ");
}

并在ProfileChangeFragment中。我用它了:

public class ProfileChangeFragment extends Fragment {
Button btn;
EditText editText;
private SQLiteHandler db;


public static final ProfileChangeFragment newInstance()
{
    ProfileChangeFragment mf = new ProfileChangeFragment();
    Bundle bd = new Bundle(1);
    mf.setArguments(bd);
    return mf;
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState )
{
   View v = inflater.inflate(R.layout.fragment_profilechange, container, false);

    btn = (Button) v.findViewById(R.id.button);
    editText = (EditText) v.findViewById(R.id.editText);
    db = new SQLiteHandler(getActivity().getApplicationContext());

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        // Get text which i want to set in table
        String name = editText.getText().toString().trim();

        // Get uid with getUserDetails method
        HashMap<String, String> user = db.getUserDetails();
        String uid = user.get("uid");

        // Now update       
        db.updateUsername(uid,name);

        }
    });
    return v;
}

当我点击按钮时,应用程序崩溃了。也许是因为OnClick方法无法处理所有数据库内容?

1 个答案:

答案 0 :(得分:0)

LIMIT 0,以便返回无结果。它应该是LIMIT 1