i am Learning Database Storage in Android for that i am trying to create a Simple App With Names of vegetables and Friuts and Flowers for that
i designed a database like this
My Xml Design Was Like this From Here i am Creating a Table Name( Vegetable ) and table image from user Input and saving that into my Database
When i Click the Done Button I cant able to add a table in my Database it Shows "Failed to create" Toast Msg was Display
My Database Class :
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "checkslate.db";
public static final String TABLE_NAME ="listview_name";
public static final String COLUMN_ID="ID";
public static final String COLUMN_TITLE="ITEM_NAME";
private static final String COLUMN_IMAGE = "image_bitmap";;
private Context context;
public DatabaseHelper(Context context)
{
super(context,DATABASE_NAME,null,1);
this.context=context;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String query =
"CREATE TABLE " +TABLE_NAME +"("
+ COLUMN_ID + " INTEGER PRIMARY KEY ,"
+ COLUMN_TITLE + " TEXT, "
+ COLUMN_IMAGE + " BLOB);";
sqLiteDatabase.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}
void createlist(String title, byte[] image)
{
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("COLUMN_TITLE", title);
cv.put("COLUMN_IMAGE", image);
Long result = sqLiteDatabase.insert( TABLE_NAME, null, cv );
if (result==-1)
{
Toast.makeText(context, "Failed to create", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(context, "Created Sucessfully", Toast.LENGTH_SHORT).show();
}
}
}
My Activity Class
public class NewCreate extends BottomSheetDialogFragment {
int[] images = {R.drawable.carrot, R.drawable.fruits, R.drawable.pineaaple, R.drawable.beetroot, R.drawable.cabbage, R.drawable.Orange};
int imageRes = images[0];
private Bitmap bitmap;
public NewListCreate() {
}
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.new_list_create, container, false);
ImageButton done = view.findViewById(R.id.done);
final EditText listname = (EditText) view.findViewById(R.id.listname);
final GridView gridView = (GridView) view.findViewById(R.id.gridview);
final MyCustomAdpter customAdpter = new MyCustomAdpter(images, getContext());
gridView.setAdapter(customAdpter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
customAdpter.selectedImage = i;
customAdpter.notifyDataSetChanged();
imageRes = images[i];
}
});
done.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String itemname = listname.getText().toString().trim();
Bitmap image = BitmapFactory.decodeResource(getResources(),
imageRes);
// convert bitmap to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte imageInByte[] = stream.toByteArray();
if (!TextUtils.isEmpty(listname.getText().toString())) {
DatabaseHelper db = new DatabaseHelper(getContext());
db.createlist(itemname,imageInByte);
// startActivity(new Intent(getContext(), CheckslateHome.class).putExtra("data", itemname).putExtra("image", imageRes));
// dismiss();
} else {
Toast.makeText(getContext(), "List Name not Empty ", Toast.LENGTH_SHORT).show();
}
}
});
return view;
}
public class MyCustomAdpter extends BaseAdapter {
public int selectedImage = 0;
private int[] icons;
private Context context;
private LayoutInflater layoutInflater;
public MyCustomAdpter(int[] icons, Context context) {
this.icons = icons;
this.context = context;
this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return icons.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
view = layoutInflater.inflate(R.layout.image_list, viewGroup, false);
}
ImageView imageicons = view.findViewById(R.id.image);
if (i < icons.length) {
imageicons.setImageResource(icons[i]);
if (i != selectedImage) {
imageicons.setImageAlpha(50);
}
imageicons.setScaleType(ImageView.ScaleType.CENTER_CROP);
// imageicons.setLayoutParams(new GridView.LayoutParams(150, 150));
if (i == selectedImage) {
view.setBackgroundColor(Color.WHITE);
} else {
view.setBackgroundColor(Color.TRANSPARENT);
}
}
;
return view;
}
}
error :
android.database.sqlite.SQLiteException: table listview_name has no column named COLUMN_TITLE (code 1 SQLITE_ERROR): , while compiling: INSERT INTO listview_name(COLUMN_TITLE,COLUMN_IMAGE) VALUES (?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1569)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1440)
at com.Karthickyuvan.checkslate.DatabaseHelper.createlist(DatabaseHelper.java:51)
at com.Karthickyuvan.checkslate.NewListCreate$2.onClick(NewListCreate.java:92)
at android.view.View.performClick(View.java:6724)
at android.view.View.performClickInternal(View.java:6682)
at android.view.View.access$3400(View.java:797)
at android.view.View$PerformClick.run(View.java:26479)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:226)
at android.app.ActivityThread.main(ActivityThread.java:7225)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:499)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:962)
See Question&Answers more detail:
os