SQLite操作

前端之家收集整理的这篇文章主要介绍了SQLite操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Android提供了创建和是用sqlite数据库的API。sqliteDatabase代表一个数据库对象,提供了操作数据库的一些方法

要求:

使用sqlite数据库,将某条新闻(包含标题时间内容)插入到数据库中。

实现:

sqliteOpenHelper类是sqliteDatabase一个辅助类。这个类主要生成一 个数据库,并对数据库的版本进行管理。当在程序当中调用这个类的方法getWritableDatabase()或者 getReadableDatabase()方法的时候,如果当时没有数据,那么Android系统就会自动生成一个数据库

package com.example.hades.toutiao;

import android.content.Context;
import android.database.sqlite.sqliteDatabase;
import android.database.sqlite.sqliteOpenHelper;
import android.util.Log;

/** * Created by hades on 2017/6/17. */

public class NewsListDB extends sqliteOpenHelper {

    public NewsListDB(Context context,String name,sqliteDatabase.CursorFactory factory,int version) {
        super(context,name,factory,version);
    }

    public void onCreate(sqliteDatabase db) {
        String sql = "create table if not exists list(_id integer primary key autoincrement,title text,desc text,content text,pubdate text,img text)";
        Log.i("Tag","create Database");
        db.execsql(sql);
    }

    public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
        Log.i("Tag","update Databases");
    }
}

创建

public void Creat() {
        NewsListDB listDB = new NewsListDB(context,"news.db",null,2);
        sqliteDatabase db = listDB.getReadableDatabase();
        Log.i("Tag","Creat");
    }

升级

public void Update() {
        NewsListDB listDB = new NewsListDB(context,"Update");
    }

插入
注意corsor的关闭

public void Insert(String t,String d,String c,String i,String p) {
        NewsListDB listDB = new NewsListDB(context,null,2);
        sqliteDatabase db = listDB.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("title",t);
        cv.put("desc",d);
        cv.put("content",c);
        cv.put("img",i);
        cv.put("pubdate",p);
        db.insertOrThrow("list",cv);
        Log.i("Tag","Indert");
        db.close();
    }

查询
返回一个map对象

public List<Map<String,String>> Query() {
        NewsListDB dbHelper = new NewsListDB(context,null,2);
        sqliteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query("list",new String[]{"title","desc","content","img","pubdate"},null);
        List<Map<String,String>> data = new ArrayList<Map<String,String>>();
        while (cursor.moveToNext()) {
            Map map = new HashMap<String,String>();
            map.put("news_title",cursor.getString(cursor.getColumnIndex("title")));
            map.put("news_desc",cursor.getString(cursor.getColumnIndex("desc")));
            map.put("news_img",cursor.getString(cursor.getColumnIndex("img")));
            map.put("news_content",cursor.getString(cursor.getColumnIndex("content")));
            map.put("news_pubdate",cursor.getString(cursor.getColumnIndex("pubdate")));
            data.add(map);
        }
        cursor.close();
        db.close();
        return data;
    }

删除

public void Delete() {
        NewsListDB listDB = new NewsListDB(context,2);
        sqliteDatabase db = listDB.getReadableDatabase();
        String whereClauses = "id=?";
        String[] whereArgs = {String.valueOf(2)};
        db.delete("stu_table",whereClauses,whereArgs);
    }

完整AddLikeItem类:

package com.example.hades.toutiao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.sqliteDatabase;
import android.database.sqlite.sqliteOpenHelper;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** * Created by hades on 2017/6/17. */

public class AddLikeItem {
    public Context context;
    public String newsTitle,newsDesc,newsContent,newsImg,newsPubDate;


    public AddLikeItem(Context context) {
        this.context = context;
        Log.i("Tag","context2" + String.valueOf(context));

    }


    public void Creat() {
        NewsListDB listDB = new NewsListDB(context,"Creat");
    }

    public void Update() {
        NewsListDB listDB = new NewsListDB(context,"Update");
    }

    public void Insert(String t,String d,String c,String i,String p) {
        NewsListDB listDB = new NewsListDB(context,2);
        sqliteDatabase db = listDB.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("title",t);
        cv.put("desc",d);
        cv.put("content",c);
        cv.put("img",i);
        cv.put("pubdate",p);
        db.insertOrThrow("list",cv);
        Log.i("Tag","Indert");
        db.close();
    }

    public List<Map<String,String>> Query() {
        NewsListDB dbHelper = new NewsListDB(context,new String[]{"title",null);
        List<Map<String,String>> data = new ArrayList<Map<String,String>>();
        while (cursor.moveToNext()) {
            Map map = new HashMap<String,String>();
            map.put("news_title",cursor.getString(cursor.getColumnIndex("pubdate")));
            data.add(map);
        }
        cursor.close();
        db.close();
        return data;
    }

    public void Delete() {
        NewsListDB listDB = new NewsListDB(context,whereArgs);
    }
}

猜你在找的Sqlite相关文章