دیتابیس SQLite

گزارش
سوال

لطفا علت گزارش خود را توضیح دهید.

گزارش لغو

سلام

روز بخیر

من بعد از کپی دیتابیس و ساخت کلاس SQLiteOpenHelper وقتی سورسش رو می نویسم به متدد هایه Cursor میرسم

public Cursor getStroy(int id){
String query = \”SELECT * FROM \”+story_tbl+\” WHERE id = \’\”+id+\”\’\”;
return database.rawQuery(query,null);
}

public Cursor getAll(int category){
String query = \”SELECT * FROM \”+story_tbl+\” WHERE \”+ DBOpenHelper.category +\” = \’\”+category+\”\’ ORDER BY id DESC\”;
return database.rawQuery(query,null);
}

public void setFav(int id,int fav){
String sql = \”UPDATE \”+story_tbl+\” SET \”+favorite+\” = \”+fav+\” WHERE \”+ DBOpenHelper.id +\” = \”+id+\” ;\”;
database.execSQL(sql);
}

public Cursor getFav(){
String query = \”SELECT * FROM \”+story_tbl+\” WHERE \”+favorite+\” = \’1\’ \”;
return database.rawQuery(query,null);
}

public Cursor searchFav(String s){
String query = \”SELECT * FROM \”+story_tbl+\” WHERE \”+story+\” like \’%\”+s+\”%\’ AND \”+favorite+\” = 1 ;\”;
return database.rawQuery(query,null);
}

public Cursor search(int cate,String s){
String query = \”SELECT * FROM \”+story_tbl+\” WHERE \”+category+\” = \”+cate+\” AND \”+story+\” like \’%\”+s+\”%\’ ;\”;
return database.rawQuery(query,null);
}

این متدد ها فعال نمی شن کلماتWHERE,FROM,SELECT به رنگ ابی

می مونن

من باید کد خاصی رو در ساخت دیتابیس در DB Browser (SQLite) وارد کنم یا

کار  دیگه ای باید کنم …

یه اموزش ویدیویی در فرانش برایه سخت کتاب

…………………………..

سورس کامل

package ir.hetbo.book_java.database;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
* Created by Hetbo on 1/8/2018.
*/

public class DBOpenHelper extends SQLiteOpenHelper {

private static int db_version = 1;
static String db_name = \”book\”;
static String db_path = \”/data/data/ir.hetbo.book_java/databases/\”;
private Context context;
private SQLiteDatabase database;

final static String story_tbl = \”tbl1\”;
final static String id = \”id\”;
final static String title = \”onvan\”;
final static String category = \”daste\”;
final static String favorite = \”fav\”;
final static String story = \”matn1\”;

public DBOpenHelper(Context context) {
super(context, db_name, null, db_version);
this.context = context;
}

private boolean checkExist() {
SQLiteDatabase db = null;
try{
db = SQLiteDatabase.openDatabase(db_path+db_name, null, SQLiteDatabase.OPEN_READONLY);
}
catch(SQLException e) {}
return db != null;
}

private void copyDatabase() throws IOException {
OutputStream myOutput = new FileOutputStream(db_path+db_name);
byte[] buffer = new byte[1024];
int length;
InputStream myInput = context.getAssets().open(db_name+\”.db\”);
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myInput.close();
myOutput.flush();
myOutput.close();
}

public void importIfNotExist() throws IOException {
boolean dbExist = checkExist();
if (!dbExist) {
this.getReadableDatabase();
try {
copyDatabase();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public void open(){
database = SQLiteDatabase.openDatabase(db_path+db_name, null, SQLiteDatabase.OPEN_READWRITE);
}

public void close(){
database.close();
}

public Cursor getStroy(int id){
String query = \”SELECT * FROM \”+story_tbl+\” WHERE id = \’\”+id+\”\’\”;
return database.rawQuery(query,null);
}

public Cursor getAll(int category){
String query = \”SELECT * FROM \”+story_tbl+\” WHERE \”+ DBOpenHelper.category +\” = \’\”+category+\”\’ ORDER BY id DESC\”;
return database.rawQuery(query,null);
}

public void setFav(int id,int fav){
String sql = \”UPDATE \”+story_tbl+\” SET \”+favorite+\” = \”+fav+\” WHERE \”+ DBOpenHelper.id +\” = \”+id+\” ;\”;
database.execSQL(sql);
}

public Cursor getFav(){
String query = \”SELECT * FROM \”+story_tbl+\” WHERE \”+favorite+\” = \’1\’ \”;
return database.rawQuery(query,null);
}

public Cursor searchFav(String s){
String query = \”SELECT * FROM \”+story_tbl+\” WHERE \”+story+\” like \’%\”+s+\”%\’ AND \”+favorite+\” = 1 ;\”;
return database.rawQuery(query,null);
}

public Cursor search(int cate,String s){
String query = \”SELECT * FROM \”+story_tbl+\” WHERE \”+category+\” = \”+cate+\” AND \”+story+\” like \’%\”+s+\”%\’ ;\”;
return database.rawQuery(query,null);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}
}

 

 

 

 

 

 

 

 

 

 

0
اندروید - android 0 پاسخ 115 بازدید 0

درباره نویسنده

ارسال نظر