Android:代码1 SQLITE_ERROR(未找到Colum_name) [英] Android : code 1 SQLITE_ERROR (No Colum_name was Found)

查看:93
本文介绍了Android:代码1 SQLITE_ERROR(未找到Colum_name)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为此学习Android中的数据库存储,为此我正在尝试创建一个具有蔬菜,水果和鲜花名称的简单应用程序

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

我设计了这样的数据库

我的Xml设计从这里是这样的,我正在从用户输入创建一个表名(Vegetable)和表图像,并将其保存到我的数据库中

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

我的数据库类:

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();
                }
    }
}

我的活动班

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;
        }
    }

错误:

 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)

推荐答案

如果已为列名定义了常量,则应使用它们.在这里,您将它们作为字符串传递,这就是为什么在向表中插入数据时出错.

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.

在您的createlist方法中,使用下面的

In your createlist method use below

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

代替您使用的是

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

这篇关于Android:代码1 SQLITE_ERROR(未找到Colum_name)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆