2015-06-04 129 views
1

我正在做一个项目,我需要从sqlite数据库检索数据,但无法做到这一点。请帮助我解决问题。我无法从数据库检索数据库从sqlite没有php或任何

我登录代码

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(); 
    } 

} 

好心通过它检查。 我想从数据库中检索数据并检查,但它没有执行该操作。

+0

你是什么意思“没有根据执行操作“?数据库中没有数据?有些方法没有被调用?你会得到一个异常? –

+0

我有数据库,甚至值,我没有得到值和条件checked.And也没有提出任何怀疑 – user2794456

+0

当你调用openOrCreateDatabase(),你应该以静态的方式调用它:SQLiteDatabase.openOrCreateDatabase()。你的代码甚至编译是很奇怪的。 – JDenais

回答

-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; 
    } 
+0

我可以知道有关databasehelper – user2794456

+0

B2MDatabaseHelper是一个类,您可以创建SQLite数据库 –

0

如果你想要从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(); 
     } 
    } 

在SQLite数据库类最后使用:

public String getSinUserlgeEntry(String userName, String Id) { 
     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, emp_name + " LIKE '%" + userName + "%' AND " + main_IDD + " LIKE '%" + Id + "%'", null, null, null, null); 
     if (cursor.getCount() < 1) // UserName Not Exist 
     { 
      cursor.close(); 
      return "NOT EXIST"; 
     } 
     cursor.moveToFirst(); 
     String iddd = cursor.getString(cursor.getColumnIndex("mid")); 
     cursor.close(); 
     return iddd; 
    }