我正在做一个项目,我需要从sqlite数据库中检索数据,但无法做到这一点。请帮助我完成。
我的登录代码
package com.example.pallavi.myproject;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class Login extends Activity{
Intent i=null;
EditText tv1,tv4;
boolean flag=false;
SQLiteDatabase db=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
tv1 = (EditText) findViewById(R.id.phone2);
tv4 = (EditText) findViewById(R.id.password2);
db = openOrCreateDatabase("mydb", MODE_PRIVATE, null);
}
public void action(View v)
{
String mobile_no=tv1.getText().toString();
String password=tv4.getText().toString();
/* if(mobile_no==null||mobile_no=="")
{
show("Please Enter Correct mobile number.");
}
else if(password==null||password=="")
{
show("Please Enter Correct Password.");
}
else*/
{
show("Query is not executing");
String[] args={"null"};
Cursor c=db.rawQuery("select * from login where mobile_no='"+mobile_no+"'",args);
c.moveToFirst();
show("not going inside");
if(c.getCount()>0)
{
show("Success fully login");
i=new Intent(this,post.class);
db.close();
finish();
}
else
show("Wrong Password or Mobile number.");
}
}
public void acc(View v)
{
if (v.getId()==R.id.button)
{
Intent i=new Intent(Login.this,MainActivity.class);
startActivity(i);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
public void show(String str)
{
Toast.makeText(this, str, Toast.LENGTH_LONG).show();
}
}
请仔细检查。 我正在尝试从db检索数据并检查,但它没有执行操作。
答案 0 :(得分:0)
如果您想从SQLite中检索数据以便登录,请使用以下代码:
#test:hover
{
background-color: blue;
}
最后在SQLite数据库类中使用它:
public void login_user(View view) {
editTextUserName = (EditText) findViewById(R.id.editText2);
editTextID = (EditText) findViewById(R.id.editText);
editTextID.setFocusable(true);
username = editTextUserName.getText().toString();
password = editTextID.getText().toString();
final String idd = password;
final String name = username;
if (password.equals("")) {
editTextID.setError("Enter User Id");
editTextID.requestFocus();
} else if (username.equals("")) {
editTextUserName.setError("Enter User Name");
editTextUserName.requestFocus();
} else {
String storedPassword = databaseHelper.getSinUserlgeEntry(username,password);
// check if the Stored password matches with Password entered by user
if (password.equals(storedPassword)) {
Toast toast = Toast.makeText(this, "Congrats: Login Successful", Toast.LENGTH_LONG);
View vieew = toast.getView();
// vieew.setBackgroundColor(Color.parseColor("#BD8BDC"));
vieew.setBackgroundResource(R.drawable.textinputborder);
toast.setView(vieew);
// TextView text = (TextView) view.findViewById(android.R.id.message);
/*here you can do anything with text*/
toast.show();
Intent intent = new Intent(this, user_home.class);
intent.putExtra("Name", username);
intent.putExtra("ID", password);
startActivity(intent);
} else {
Toast toast = Toast.makeText(this, "User Name or Password does not match", Toast.LENGTH_LONG);
View vieew = toast.getView();
// vieew.setBackgroundColor(Color.parseColor("#BD8BDC"));
vieew.setBackgroundResource(R.drawable.textinputborder);
toast.setView(vieew);
// TextView text = (TextView) view.findViewById(android.R.id.message);
/*here you can do anything with text*/
toast.show();
}
editTextUserName.setText("");
editTextID.setText("");
editTextID.requestFocus();
}
}
答案 1 :(得分:-1)
您可以使用它从Sqlite中检索数据。
首先在需要检索数据的地方使用此代码:
OurAppsPackages = new ArrayList<String>();
OurAppsPackages = databaseHelper.getOurAllAppsPackage();
在数据库的B2MDatabaseHelper类中使用波纹管代码。
public ArrayList<String> getOurAllAppsPackage() {
SQLiteDatabase db = this.getWritableDatabase();
// Cursor cursor = db.query("day_info", null, " emp_name=?", new String[]{userName}, null, null, null);
Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
cursor.moveToFirst();
ArrayList<String> pkgs= new ArrayList<String>();
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
String pkgN = cursor.getString(cursor.getColumnIndex("PackageName"));
pkgs.add(pkgN.toString());
cursor.moveToNext();
}
}
return pkgs;
}