1. 程式人生 > >SQLite資料庫(基礎)

SQLite資料庫(基礎)

建立資料庫,寫sql語句

package com.example.zk3lianxi1.sql;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MySQLite  extends SQLiteOpenHelper {
    public MySQLite(Context context) {
        super(context, "bb", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE persona(personid INTEGER PRIMARY KEY AUTOINCREMENT,news_id VARCHAR(100)," +
                "news_title VARCHAR(100)," +
                "news_summary VARCHAR(100)," +
                "pic_url VARCHAR(100))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

這個類呼叫一下MySqlite的程式碼然後寫增刪改查

package com.example.zk3lianxi1.sql;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Dao {
    private SQLiteDatabase db;
    public Dao(Context context){
            MySQLite mySQLite = new MySQLite(context);
            db = mySQLite.getWritableDatabase();
    }

    public long insert(String table, String nullColumnHack, ContentValues values)
    {
        return db.insert(table, nullColumnHack, values);
    }
    //刪除
    public long delete(String table, String whereClause, String[] whereArgs)
    {
        return db.delete(table, whereClause, whereArgs);
    }
    //修改
    public long update(String table, ContentValues values, String whereClause, String[] whereArgs)
    {
        return db.update(table, values, whereClause, whereArgs);
    }
    //查詢
    public Cursor query(String table, String[] columns,
                        String selection, String[] selectionArgs, String groupBy,
                        String having, String orderBy)
    {
        return db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
    }
}

//新增

//          Dao dao = new Dao(MainActivity.this);
//            for(int i  = 0 ; i < list.size();i++){
//                Bean.DataBean bean1 = list.get(i);
//                ContentValues values = new ContentValues();
////                private String news_id;
////                private String news_title;
////                private String news_summary;
////                private String pic_url;
//                values.put("news_id",bean1.getNews_id());
//                values.put("news_title",bean1.getNews_title());
//                values.put("news_summary",bean1.getNews_summary());
//                values.put("pic_url",bean1.getPic_url());
//                insert = dao.insert("per", null, values);
//            }
//            Toast.makeText(MainActivity.this, "新增條數"+insert, Toast.LENGTH_SHORT).show();

查詢

Dao dao = new Dao(MainActivity.this);
            Cursor query1 = dao.query("per", null, null, null, null, null, null);
            ArrayList<Bean.DataBean> list1 = new ArrayList<Bean.DataBean>();
            if(query1.moveToFirst()){
                do {
                    String title = query1.getString(query1.getColumnIndex("news_title"));
                    Bean.DataBean bean2 = new Bean.DataBean(title);
                    list1.add(bean2);
                }while (query1.moveToNext());
            }
            query1.close();
            list.addAll(list1);