انجمن سایت کلیدستان


رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
مشکل در جستجو در دیتابیس

مشکل در جستجو در دیتابیس

#1
سلام
میشه بگید مشکل کد زیر چیه که موقع اجرا ارور میده؟
کد پی‌اچ‌پی:
public class MainActivity extends ListActivity {
    
    List<
BeytbeyTha=null;
    
DBAdapter db=new DBAdapter(this);
    
String tbl_beyt=db.DB_TABLE;
    
    @
Override
    
protected void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        
setContentView(R.layout.activity_main);
        

        
        try{
            
String destPath="/data/data/"+getPackageName()+"/databases";
            
File f=new File(destPath);
            
            if(!
f.exists()){
                
f.mkdirs();
                
f.createNewFile();
                
                
CopyDB(getBaseContext().getAssets().open("laki"),
                        new 
FileOutputStream(destPath+"/laki"));
            }
        }catch (
FileNotFoundException e) {
            
e.printStackTrace();
        }catch (
IOException e) {
            
e.printStackTrace();
        }
        
        
        
db.open();
        List<
BeytbeytHa=db.getAllBeyt();
        
setListAdapter(new ArrayAdapter<Beyt>(MainActivity.this,
                
android.R.layout.simple_list_item_1beytHa));
        
db.close();
        
        
        
        
        
        final 
EditText et=(EditText)findViewById(R.id.eDitText1);
        
et.addTextChangedListener(new TextWatcher() {
            
            @
Override
            
public void onTextChanged(CharSequence sint startint beforeint count) {
                
// TODO Auto-generated method stub
                
            
}
            
            @
Override
            
public void beforeTextChanged(CharSequence sint startint count,
                    
int after) {
                
// TODO Auto-generated method stub
                
            
}
            
            @
Override
            
public void afterTextChanged(Editable s) {
                
// TODO Auto-generated method stub
                
db.findBeyt(et.getText().toString(),tbl_beyt);
                
tazeSazi();
            }
        });
        
        
        
    }

    
    
    
    public 
void CopyDB(InputStream inputStream,
            
OutputStream outputStream)throws IOException {
        
        
byte[] buffer=new byte[1024];
        
int length;
        while ((
length=inputStream.read(buffer))>0) {
            
outputStream.write(buffer,0,length);
        }
        
inputStream.close();
        
outputStream.close();
    }
    
    
    
    
    public 
void tazeSazi() {
        
ArrayAdapter<Beytadapter;
        
adapter=new ArrayAdapter<Beyt>(this,
                
android.R.layout.simple_list_item_1beyTha);
        
setListAdapter(adapter);
    }
    
    

DBAdapter:
کد پی‌اچ‌پی:
public class DBAdapter {
    static final 
String KEY_ROWID="_id";
    static final 
String KEY_BEYT="beyt";
    static final 
String TAG="DBAdapter";
    
    static final 
String DB_NAME="laki";
    static final 
String DB_TABLE="abyat";
    static final 
int DB_VERSION=1;
    
    static final 
String DB_CREATE=
            
"create table abyat (_id integer primary key autoincrement,"+
    
"beyt text not null);";
    
    final 
Context context;
    
    
DatabaseHelper DBHelper;
    
SQLiteDatabase db;
    
    
    public 
DBAdapter (Context ctx) {
        
this.context=ctx;
        
DBHelper=new DatabaseHelper(context);
    }
    
    
    
    private static class 
DatabaseHelper extends SQLiteOpenHelper {
        
DatabaseHelper(Context context){
            
super(contextDB_NAMEnullDB_VERSION);
        }
        
        
        @
Override
        
public void onCreate(SQLiteDatabase db) {
            try {
                
db.execSQL(DB_CREATE);
            } catch (
SQLException e) {
                
e.printStackTrace();
            }
        }
        
        
        
        @
Override
        
public void onUpgrade(SQLiteDatabase dbint oldVersionint newVersion) {
            
Log.w(TAG"Upgrading database from "+oldVersion+"to"+newVersion+
                    
",which will destroy all old data");
            
db.execSQL("DROP TABLE IF EXISTS abyat");
            
onCreate(db);
        }
    }
    
    
    
    public 
DBAdapter open() throws SQLException {
        
db=DBHelper.getReadableDatabase();
        return 
this;
    }
    
    
    public 
void close() {
        
DBHelper.close();
    }
    
    
    
    public 
long insertBeyt(String beyt) {
        
ContentValues initialValue=new ContentValues();
        
initialValue.put(KEY_BEYTbeyt);
        return 
db.insert(DB_TABLEnullinitialValue);
    }
    
    
    
    public 
boolean deleteBeyt(long rowId) {
        return 
db.delete(DB_TABLEKEY_ROWID+"="+rowIdnull)>0;
    }
    
    
    
    public List<
BeytgetAllBeyt() {
        
Cursor cursor=db.query(DB_TABLE,new String[]{KEY_ROWID,KEY_BEYT},
                
nullnullnullnullnull);
        List<
Beytbeytha=cursorToList(cursor);
        return 
beytha;
    }
    
    
    
    
    public 
Cursor getBeyt(long rowId)throws SQLException {
        
Cursor mCursor=db.query(true,
                
DB_TABLE, new String[]{KEY_ROWID,KEY_BEYT}, KEY_ROWID+"="+rowId,
                
nullnullnullnullnull);
        
        if(
mCursor!=null){
            
mCursor.moveToFirst();
        }
        
        return 
mCursor;
    }
    
    
    
    
    private List<
BeytcursorToList(Cursor cursor) {
        List<
BeytbeYt=new ArrayList<Beyt>();
        if(
cursor.getCount()>0){
            while (
cursor.moveToNext()) {
                
Beyt beyt=new Beyt();
                
beyt.setId(cursor.getString(0));
                
beyt.setBeyt(cursor.getString(1));
                
beYt.add(beyt);
            };
        }
        return 
beYt;
    }
    
    
    
    public List<
BeytfindBeyt(String beyt,String tabalethrows SQLException 
    
{
        
Cursor cursor =
                
// % ha yani inke mohem nis ghabl ya badesh harchi mikhad bashe
                
db.query(truetabale, new String[] {KEY_ROWID,
                
KEY_BEYT}, KEY_BEYT " LIKE '%" beyt "%'"null,
                
nullnullnullnull);
        List<
Beytbeytha cursorToList(cursor);
        return 
beytha;
    }
    
    
    
    
    public 
boolean updateBeyt(long rowId,String beyt) {
        
ContentValues args=new ContentValues();
        
args.put(KEY_BEYTbeyt);
        return 
db.update(DB_TABLEargsKEY_ROWID+"="+rowIdnull)>0;
    }

Beyt:
کد پی‌اچ‌پی:
public class Beyt implements Parcelable{

    private 
String id;
    private 
String beyt;
    
    public 
Beyt() {
    }
    
    
    public 
String getId() {
        return 
id;
    }
    
    
    public 
void setId(String id) {
        
this.id=id;
    }
    
    
    public 
String getBeyt() {
        return 
beyt;
    }
    
    
    public 
void setBeyt(String beyt) {
        
this.beyt=beyt;
    }
    
    
    
    
    @
Override
    
public String toString(){
        return 
beyt+"";
    }
    
    
    
    public 
Beyt(Parcel in) {
        
id=in.readString();
        
beyt=in.readString();
    }
    
    
    @
Override
    
public int describeContents() {
        return 
0;
    }

    @
Override
    
public void writeToParcel(Parcel destint flags) {
        
// TODO Auto-generated method stub
        
dest.writeString(id);
        
dest.writeString(beyt);
    }
    
    
    
    
    public static final 
Parcelable.Creator<Beytcreator() {
        return new 
Parcelable.Creator<Beyt>() {

            @
Override
            
public Beyt createFromParcel(Parcel source) {
                
// TODO Auto-generated method stub
                
return new Beyt(source);
            }

            @
Override
            
public Beyt[] newArray(int size) {
                
// TODO Auto-generated method stub
                
return new Beyt[size];
            }
        };
    }



وقتی یه حرف رو تایپ میکنم ارور میده
پاسخ
 سپاس شده توسط شماره مجازی امارات ، تلگرام ضد فیلتر 2023


پرش به انجمن:


کاربران در حال بازدید این موضوع: 1 مهمان