我正在创建一个登录应用程序,其中在 EditText 中键入的数据将在单击 java 文件中的按钮时进入一个变量,然后该值将存储到我创建的 sqlite 数据库中,但是当我在 EditText 中键入值和单击按钮我没有在文件资源管理器中创建任何数据库。
这是我的代码:
import java.util.Locale;
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
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 AAAActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText userid = (EditText)findViewById(R.id.et_un);
EditText password = (EditText)findViewById(R.id.et_pw);
final String uid;
final String pwd;
uid = userid.getText().toString();
pwd = password.getText().toString();
Button btn = (Button)findViewById(R.id.btn_login);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
InsertUser(uid, pwd);
show();
}
});
}
public void InsertUser(String user_name, String password){
//SQLiteDatabase db;
SQLiteDatabase db;
db = openOrCreateDatabase("TestData.db", SQLiteDatabase.CREATE_IF_NECESSARY,
null);
try{
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
/*final String CREATE_TABLE_USERS = "CREATE TABLE users (" + "id INTEGER
PRIMARY KEY AUTOINCREMENT,"+" LoginName text not null, Password
text not null);"; */
String CREATE_TABLE_USERS = "CREATE TABLE users (" + "id INTEGER PRIMARY KEY
AUTOINCREMENT,"+" LoginName text not null, Password text not null);";
db.execSQL(CREATE_TABLE_USERS);
ContentValues values = new ContentValues();
values.put("LoginName", user_name);
values.put("Password", password);
db.insert("users", null, values);
}
catch(Exception e){
}
}
public void show(){
Toast toast=Toast.makeText(this, "your data is received", Toast.LENGTH_SHORT );
toast.show();
}
}