将数据插入数据库(Android Studio)

时间:2017-05-21 14:25:37

标签: android sql database insert

我正在尝试使用您拥有帐户的应用,您必须登录才能使用该应用。显然你需要注册该应用程序。问题是当我进入注册表单并输入所有细节时,数据不会进入数据库。有什么帮助吗?

使用的参考:https://www.youtube.com/watch?v=KxlLsk5j3rY

这是将联系人插入数据库的方法。

public void insertContact(Account a)
{

    ContentValues values = new ContentValues();

    String query = "select * from users";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    int count = cursor.getCount();

    values.put(COLUMN_ID,count);
    values.put(COLUMN_FirstName, a.getFirstname());
    values.put(COLUMN_LastName, a.getLastname());
    values.put(COLUMN_Username, a.getUsername());
    values.put(COLUMN_Password, a.getPassword());
    values.put(COLUMN_Email, a.getEmail());
    values.put(COLUMN_ClubName, a.getEmail());
    values.put(COLUMN_Address, a.getAddress());


    db.insert(TABLE_NAME, null, values);
    Log.e("Contact Entered", "DATABASE");
    db.close();
}

注册按钮事件

public void onRegisterClick(View v)
{
    if(v.getId() == R.id.btnregister) {
        EditText name = (EditText) findViewById(R.id.txtname);
        EditText lastname = (EditText) findViewById(R.id.txtlastname);
        EditText username = (EditText) findViewById(R.id.txtusername);
        EditText password = (EditText) findViewById(R.id.txtpassword);
        EditText email = (EditText) findViewById(R.id.txtemail);
        EditText clubname = (EditText) findViewById(R.id.txtclubname);
        EditText address = (EditText) findViewById(R.id.txtaddress);

        //Text entered in the textview is assigned to a variable and to string
        String namestr = name.getText().toString();
        String lastnamestr = lastname.getText().toString();
        String userstr = username.getText().toString();
        String passstr = password.getText().toString();
        String emailstr = email.getText().toString();
        String clubnamestr = clubname.getText().toString();
        String addresssrt = address.getText().toString();


        Account a = new Account();
        a.setFirstname(namestr);
        a.setLastname(lastnamestr);
        a.setUsername(userstr);
        a.setPassword(passstr);
        a.setEmail(emailstr);
        a.setClubname(clubnamestr);
        a.setAddress(addresssrt);

        datab.insertContact(a);
        Log.e("insertContact(a)", "DATABASE");
        Toast temp = Toast.makeText(CreateAccount.this, "Account created!", Toast.LENGTH_SHORT);
        temp.show();

        Intent i = new Intent(CreateAccount.this, MainActivity.class);
        startActivity(i);
    }
}

2 个答案:

答案 0 :(得分:0)

您只需使用SuperDatabase即可使用纯SQL控制数据库。我已经创建了一个库,可以让Android上的内容变得更轻松。将库导入Gradle并使用SuperDatabase。

此处的文件:https://github.com/sangeethnandakumar/TestTube

答案 1 :(得分:0)

这对我有用。添加BeginTransaction()和SetTransactionSuccessful有帮助。

public void insertContact(Account a)
{

ContentValues values = new ContentValues();
db.beginTransaction();
String query = "select * from users";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
int count = cursor.getCount();

values.put(COLUMN_ID,count);
values.put(COLUMN_FirstName, a.getFirstname());
values.put(COLUMN_LastName, a.getLastname());
values.put(COLUMN_Username, a.getUsername());
values.put(COLUMN_Password, a.getPassword());
values.put(COLUMN_Email, a.getEmail());
values.put(COLUMN_ClubName, a.getEmail());
values.put(COLUMN_Address, a.getAddress());


db.insert(TABLE_NAME, null, values);
Log.e("Contact Entered", "DATABASE");
db.setTransactionSuccessful();
db.endTransaction();
db.close();
}