Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
628 views
in Technique[技术] by (71.8m points)

image - Android : code 1 SQLITE_ERROR (No Colum_name was Found)

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

enter image description here

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 enter image description here

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you have defined the constants for column names, then you should use them. Here you are passing them as a string and that's why you are getting an error while inserting data into the table.

In your createlist method use below

cv.put(COLUMN_TITLE, title);
cv.put(COLUMN_IMAGE, image);

instead of what you are using:

cv.put("COLUMN_TITLE", title);
cv.put("COLUMN_IMAGE", image);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...