zoukankan      html  css  js  c++  java
  • 数据库SQLiteOpenHelper自创

    增:insert into 表名 (字段名,字段名) values (?,?,?)
    删:delete from 表名 where 字段=?
    改:update 表名 set 字段=?,字段=? where 条件=?
    查:select * form 表名 where 字段=?
    (*可以更改为想要查找的内容)

     创建数据库

    package com.bawei.zsgcactivity;
    
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteDatabase.CursorFactory;
    import android.database.sqlite.SQLiteOpenHelper;
    
    public class MySqlistHelper extends SQLiteOpenHelper{
    
        public MySqlistHelper(Context context, String name, CursorFactory factory,
                int version) {
            super(context, name, factory, version);
            // TODO Auto-generated constructor stub
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL("create  table good (id integer primary key autoincrement,name char(50) not null, price char(50),producter char(50),color char(50) )");
            db.execSQL("insert into good values(null,'小米','1000','北京','黄色')");    
            db.execSQL("insert into good values(null,'华为','1000','上海','红色')");    
            db.execSQL("insert into good values(null,'苹果','1000','深圳','白色')");    
    
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            
        }
    
    }

    主方法调用


    MySqlistHelper helper=new MySqlistHelper(this, "07a", null, 1);
    SQLiteDatabase   dp = helper.getReadableDatabase();

    查询

     Cursor  c = dp.rawQuery("select * from good", null);
          
            while (c.moveToNext()) {
                int id = c.getInt(c.getColumnIndex("id"));
                String name=c.getString(c.getColumnIndex("name"));
                String price=c.getString(c.getColumnIndex("price"));
                String producter=c.getString(c.getColumnIndex("producter"));
                String color=c.getString(c.getColumnIndex("color"));
                list.add(new Goods(id, name, price, producter, color));
            
            }

    修改

    MySqlistHelper  helper=new MySqlistHelper(Xiougai.this, "07a", null, 1);
                  dp = helper.getReadableDatabase();
                 dp.execSQL("update good set name=?,price=?,producter=?,color=? where id=?", new Object[]{name1,price1,producter1,color1,id}); 

    增加

    dp.execSQL("insert into good values(null,?,?,?,?)", new Object[]{name,price,producter,color});

    删除

    dp.execSQL("delete from good where id=?", new Object[]{list.get(position).getId()});

  • 相关阅读:
    【CF 359B】Permutation
    如何更新 DevC++ 的编译器
    【LG 2801】教主的魔法
    矩阵浅谈
    NOI 系列赛常见技术问题整理
    Treap 浅谈
    DP 优化浅谈
    友链
    【CF 708C】Centroids
    我跳过的坑
  • 原文地址:https://www.cnblogs.com/1426837364qqcom/p/5112701.html
Copyright © 2011-2022 走看看