나는 username과 passWord를 가져 오기 위해 어떤 실수를했는지 혼란 스럽네요. (여기 새로 왔고 android에 대해 :) 정말 미안하다. 질문) 다음은, 임의 업데이트시 아래 다음 전화를 삭제하거나 쿼리를 삽입해야 android에서 SQLite로부터 데이터를 가져오고 표시하는 방법
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;
public class LoginDataBaseAdapter
{
static final String DATABASE_NAME = "login.db";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 1;
// TODO: Create public field for each column in your table.
// SQL Statement to create a new database.
static final String DATABASE_CREATE = "create table "+"LOGIN"+ "(" +"ID"+" integer primary key autoincrement,"+ "USERNAME text,PASSWORD text,MOBILENO int); ";
// Variable to hold the database instance
public SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
// Database open/upgrade helper
private DataBaseHelper dbHelper;
public LoginDataBaseAdapter(Context _context)
{
context = _context;
dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public LoginDataBaseAdapter open() throws SQLException
{
db = dbHelper.getWritableDatabase();
return this;
}
public void close()
{
db.close();
}
public SQLiteDatabase getDatabaseInstance()
{
return db;
}
public void insertEntry(String userName,String password,String mobileNumber)
{
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("USERNAME", userName);
newValues.put("PASSWORD", password);
newValues.put("MOBILENO", mobileNumber);
// Insert the row into your table
db.insert("LOGIN", null, newValues);
//Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}
public int deleteEntry(String UserName)
{
//String id=String.valueOf(ID);
String where="USERNAME=?";
int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{UserName}) ;
Toast.makeText(context, "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
return numberOFEntriesDeleted;
}
public String getSingleEntry(String userName)
{
Cursor cursor=db.query("LOGIN", null, " USERNAME=?", new String[]{userName}, null, null, null);
if(cursor.getCount()<1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST...!!!";
}
cursor.moveToFirst();
String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
// String mobileNumber=cursor.getString(cursor.getColumnIndex("MOBILENO"));
cursor.close();
return "Password="+password;
}
public void updateEntry(String userName,String password,String mobileNumber)
{
// Define the updated row content.
ContentValues updatedValues = new ContentValues();
// Assign values for each row.
updatedValues.put("USERNAME", userName);
updatedValues.put("PASSWORD", password);
updatedValues.put("MOBILENO", mobileNumber);
String where="USERNAME = ?";
db.update("LOGIN",updatedValues, where, new String[]{userName});
}
}
LoginDatabaseAdapter.java
및 SignupActivity.javaimport android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SignUpActivity extends Activity
{
EditText editTextUserName,editTextPassword,editTextConfirmPassword,editTextMobileNo;
Button btnCreateAccount;
Button btnfetch;
TextView sv;
LoginDataBaseAdapter loginDataBaseAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
// get Instance of Database Adapter
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
// Get Refferences of Views
editTextUserName=(EditText)findViewById(R.id.editTextUserName);
editTextPassword=(EditText)findViewById(R.id.editTextPassword);
editTextConfirmPassword=(EditText)findViewById(R.id.editTextConfirmPassword);
editTextMobileNo=(EditText)findViewById(R.id.editTextMobileNo);
btnCreateAccount=(Button)findViewById(R.id.buttonCreateAccount);
btnCreateAccount.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String userName=editTextUserName.getText().toString();
String password=editTextPassword.getText().toString();
String confirmPassword=editTextConfirmPassword.getText().toString();
String mobileNumber=editTextMobileNo.getText().toString();
// check if any of the fields are vaccant
if(userName.equals("")||password.equals("")||confirmPassword.equals("")||mobileNumber.equals(""))
{
Toast.makeText(getApplicationContext(), "Please Enter Your Details", Toast.LENGTH_LONG).show();
return;
}
// check if both password matches
if(!password.equals(confirmPassword))
{
Toast.makeText(getApplicationContext(), "Confirm Password does not match", Toast.LENGTH_LONG).show();
return;
}
else
{
// Save the Data in Database
loginDataBaseAdapter.insertEntry(userName, password,mobileNumber);
Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
}
}
});
btnfetch=(Button)findViewById(R.id.btnshow);
btnfetch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sv=(TextView)findViewById(R.id.tvshow);
String userName=editTextUserName.getText().toString();
String pass=loginDataBaseAdapter.getSingleEntry(userName);
sv.setText(pass);
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
loginDataBaseAdapter.close();
}
}
콘텐츠 공급자를 사용하는 것이 더 낫다는 것을 잊었고 CursorAdapters와 LoaderCallBacks와 함께 할 수 있습니다 :) –
'catch '를 사용하면 실제로 예외를 처리하지 않을 때 나쁜 생각입니다. 그리고이 질문에 어떻게 대답합니까? –
오류 잡기는 내가 잡기를 원하는지 결정할 필요가 없으며 향후 삽입, 업데이트 및 삭제, 콘텐츠 공급자 또는 ActiveAndroid와 같은 ORM 사용을 해결하는 데 도움이됩니다. 단지 사용하는 것보다 더 나은 방법입니다. SQLiteDatabase. 그 대답은 당신이 조언과 함께하는 일이며, 현재와 미래의 문제는 해결 될 것입니다. –