Learning external Sqlite With Multiple Tables,
I have 4 Fields in External SQlite and i am planning to bring all the fields in Listview
i sucessfully binds the first field in listview But when i click the first Field in listview i want to display the partficular Subfield values related to that field example my first field is BREAKFAST when i click Breakfast in Listview i want to get details like(IDLY,DOSA,PONGAL) only But now what i get was when i click BREAKFAST all the Fields in Subcategory was displayed how can we correct that help plz
Subcategory:
Subcateory.class
public class Subcategory extends ActionBarActivity {
ListView listView;
String title;
SqlLiteDbHelper dbHelper;
FoodSupply foodSupply;
SQLiteDatabase sqLiteDatabase;
Cursor cursor;
SubcategoryAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subcategory_layout);
listView= (ListView) findViewById(R.id.listView2);
Intent i = getIntent();
title = i.getStringExtra("title");
dbHelper = new SqlLiteDbHelper(this);
try {
dbHelper.openDataBase();
} catch (SQLException e) {
e.printStackTrace();
}
sqLiteDatabase=dbHelper.getReadableDatabase();
cursor=dbHelper.getsubcategory(sqLiteDatabase);
adapter= new SubcategoryAdapter(getApplicationContext(),R.layout.sublist_item);
listView.setAdapter(adapter);
if (cursor.moveToFirst())
{
do {
String subcategory;
subcategory=cursor.getString(0);
foodSupply= new FoodSupply(subcategory);
adapter.add(foodSupply);
}while (cursor.moveToNext());
}
}
public class MainActivity extends ActionBarActivity {
ListView listView;
SqlLiteDbHelper dbHelper;
FoodSupply foodSupply;
SQLiteDatabase sqLiteDatabase;
Cursor cursor;
TitleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= (ListView) findViewById(R.id.listView);
dbHelper = new SqlLiteDbHelper(this);
try {
dbHelper.openDataBase();
} catch (SQLException e) {
e.printStackTrace();
}
//foodSupply= new FoodSupply();
sqLiteDatabase=dbHelper.getReadableDatabase();
cursor=dbHelper.gettitles(sqLiteDatabase,title);
adapter= new TitleAdapter(getApplicationContext(),R.layout.list_item);
listView.setAdapter(adapter);
if (cursor.moveToFirst())
{
do {
String title;
title=cursor.getString(0);
foodSupply= new FoodSupply(title);
adapter.add(foodSupply);
}while (cursor.moveToNext());
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this,Subcategory.class);
String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
intent.putExtra("title", title);
startActivity(intent);
}
});
}
Database Class public class SqlLiteDbHelper extends SQLiteOpenHelper{
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "Food.sqlite";
private static final String DB_PATH_SUFFIX = "/databases/";
static Context ctx;
public static final String KEY_TITLE="Title";
public SqlLiteDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
ctx = context;
}
public void CopyDataBaseFromAsset() throws IOException {
InputStream myInput = ctx.getAssets().open(DATABASE_NAME);
// Path to the just created empty db
String outFileName = getDatabasePath();
// if the path doesn't exist first, create it
File f = new File(ctx.getApplicationInfo().dataDir + DB_PATH_SUFFIX);
if (!f.exists())
f.mkdir();
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
private static String getDatabasePath() {
return ctx.getApplicationInfo().dataDir + DB_PATH_SUFFIX + DATABASE_NAME;
}
public SQLiteDatabase openDataBase() throws SQLException {
File dbFile = ctx.getDatabasePath(DATABASE_NAME);
if (!dbFile.exists()) {
try {
CopyDataBaseFromAsset();
System.out.println("Copying sucess from Assets folder");
} catch (IOException e) {
throw new RuntimeException("Error creating source database", e);
}
}
return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.CREATE_IF_NECESSARY);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor gettitles(SQLiteDatabase db)
{
db = this.getReadableDatabase();
Cursor cursor;
//cursor=db.rawQuery("SELECT Title FROM food_details",null);
cursor = db.query(true, "food_details", new String[]{"Title"}, null, null, null, null, null, null);
return cursor;
}
public Cursor getsubcategory(SQLiteDatabase db,String mealname)
{
db = this.getReadableDatabase();
Cursor cursor;
cursor = db.query("food_details", new String[]{"subcategory"},KEY_TITLE + "=" + mealname, null, null, null, null);
return cursor;
}enter code here
LogCat:
Process: com.example.ky.exfoodsql, PID: 22587
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aeiltech.exfoodsql/com.example.aeiltech.exfoodsql.Subcategory}: android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: SELECT subcategory FROM food_details WHERE Title=
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2498)
at android.app.ActivityThread.access$900(ActivityThread.java:179)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5641)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1288)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1104)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: SELECT subcategory FROM food_details WHERE Title=
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1113)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:690)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1448)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1295)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1166)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1334)
at com.example.aeiltech.exfoodsql.SqlLiteDbHelper.getsubcategory(SqlLiteDbHelper.java:116)
at com.example.aeiltech.exfoodsql.Subcategory.onCreate(Subcategory.java:45)
at android.app.Activity.performCreate(Activity.java:5484)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400)
Aucun commentaire:
Enregistrer un commentaire