zoukankan      html  css  js  c++  java
  • SQLite数据库


    SQLiteOpenHeleper
    public class MySQLiteOpenHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "new.db";
    private static final int VERSION = 1;

    //创建库
    public MySQLiteOpenHelper(Context context) {
    super(context, DB_NAME, null, VERSION);
    }

    //创建表 第一次创建时调用,之后再不调用
    @Override
    public void onCreate(SQLiteDatabase db) {
    String sql = Constant.NewsTable.getCreateTableSQL();
    db.execSQL(sql);
    }
    //更新表
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("drop table " + Constant.NewsTable.TBL_NAME);
    onCreate(db);
    }
    }
    工具类,SQL语句
    public class Constant {
    public static class NewsTable{
    public static final String TBL_NAME = "NEWS";
    public static final String TBL_COL_TITLE = "NEWSTITLE";
    public static final String TBL_COL_IMG = "NEWSIMG";
    public static final String TBL_COL_SRC = "NEWSSRC";
    public static final String TBL_COL_COMM = "NEWSCOMM";
    public static final String TBL_COL_DATE = "NEWSDATE";

    public static String getCreateTableSQL(){
    String sql = "CREATE TABLE IF NOT exists "
    + TBL_NAME
    + "("
    + " _id integer primary key autoincrement ,"
    + TBL_COL_TITLE + " text,"
    + TBL_COL_IMG + " integer,"
    + TBL_COL_SRC + " text,"
    + TBL_COL_COMM + " text,"
    + TBL_COL_DATE + " varchar(50)"
    + ")";
    return sql;
    }
    }
    }


    增删改查
    public void insertdata(){
    //调用帮助类的构造函数,去创建数据库
    MySQLiteOpenHelper mySQLiteOpenHelper = new MySQLiteOpenHelper(this);
    //调用帮助类的onCreate方法,去创建表
    SQLiteDatabase sqLiteDatabase = mySQLiteOpenHelper.getWritableDatabase();
    //准备要保存的数据
    ContentValues contentValues = new ContentValues();
    contentValues.put(Constant.NewsTable.TBL_COL_TITLE,text1.getText().toString());
    contentValues.put(Constant.NewsTable.TBL_COL_IMG,text2.getText().toString());
    contentValues.put(Constant.NewsTable.TBL_COL_SRC,text3.getText().toString());
    contentValues.put(Constant.NewsTable.TBL_COL_COMM,text4.getText().toString());
    contentValues.put(Constant.NewsTable.TBL_COL_DATE,text5.getText().toString());
    //调用insert方法,去保存数据
    sqLiteDatabase.insert(Constant.NewsTable.TBL_NAME,null,contentValues);
    //关闭sqLiteDatabase数据库
    sqLiteDatabase.close();
    Toast.makeText(getApplication(),"保存数据成功",Toast.LENGTH_SHORT).show();
    /*listView = (ListView) findViewById(R.id.SQLite);
    list = new ArrayList<Map<String,String>>();
    Map<String,String> map1 = new HashMap<String, String>();
    map1.put("title",text1.getText().toString());
    map1.put("img",text2.getText().toString());
    map1.put("src",text3.getText().toString());
    map1.put("comm",text4.getText().toString());
    map1.put("date",text5.getText().toString());
    list.add(map1);*/
    }
    public void querydata(){
    MySQLiteOpenHelper mySQLiteOpenHelper = new MySQLiteOpenHelper(this);
    SQLiteDatabase sqLiteDatabase = mySQLiteOpenHelper.getReadableDatabase();
    Cursor cursor = sqLiteDatabase.query(
    Constant.NewsTable.TBL_NAME,
    new String[]{Constant.NewsTable.TBL_COL_TITLE,Constant.NewsTable.TBL_COL_DATE},
    null,null,null,null,null);
    while (cursor.moveToNext()){
    int titleIndex = cursor.getColumnIndex(Constant.NewsTable.TBL_COL_TITLE);
    String title = cursor.getString(titleIndex);
    int dateIndex = cursor.getColumnIndex(Constant.NewsTable.TBL_COL_DATE);
    String date = cursor.getString(dateIndex);
    Toast.makeText(getApplication(),title+date,Toast.LENGTH_SHORT).show();
    text1.setText(title);
    text5.setText(date);
    }
    cursor.close();
    sqLiteDatabase.close();
    Toast.makeText(getApplication(),"查询成功!!!",Toast.LENGTH_SHORT).show();
    }
    public void deletedata(){
    MySQLiteOpenHelper mySQLiteOpenHelper = new MySQLiteOpenHelper(this);
    SQLiteDatabase sqLiteDatabase = mySQLiteOpenHelper.getWritableDatabase();
    sqLiteDatabase.delete(
    Constant.NewsTable.TBL_NAME,
    Constant.NewsTable.TBL_COL_TITLE+" = ? ",
    new String[]{text1.getText().toString()}
    );
    sqLiteDatabase.close();
    Toast.makeText(getApplication(),"删除成功!!",Toast.LENGTH_SHORT).show();

    }
    public void updatedata(){
    MySQLiteOpenHelper mySQLiteOpenHelper = new MySQLiteOpenHelper(this);
    SQLiteDatabase sqLiteDatabase = mySQLiteOpenHelper.getWritableDatabase();
    String title = text1.getText().toString();
    ContentValues contentValues = new ContentValues();
    contentValues.put(Constant.NewsTable.TBL_COL_TITLE,title);
    sqLiteDatabase.update(
    Constant.NewsTable.TBL_NAME,
    contentValues,
    Constant.NewsTable.TBL_COL_TITLE+" = ? ",
    new String[]{text1.getText().toString()}
    );
    text1.setText(title);
    sqLiteDatabase.close();
    Toast.makeText(getApplication(),"更改完成!!",Toast.LENGTH_SHORT).show();
    }
  • 相关阅读:
    springboot maven
    基于spring http服务远程调用
    java注解
    mongdob-高可用集群
    Mongodb学习笔记--分片集群
    Mongodb学习笔记--springboot中使用mongodb
    Mongodb学习笔记--用户和权限
    Mongodb学习笔记--使用java连接操作mongodb
    mongodb学习笔记--集群配置(二)-副本集配置
    mongodb学习笔记--集群配置(一)
  • 原文地址:https://www.cnblogs.com/ye34166684/p/5888223.html
Copyright © 2011-2022 走看看