MySQL:
開啟服務器:net start mysql
關閉服務器:net stop mysql
登入服務器:mysql -uroot -p591740 -hlocalhost
> -u ?后面跟用戶名
> -p ?后面跟密碼
> -h后面跟IP
查看MySQL數據庫編碼:SHOW ?VARIABLES ?LIKE ?‘char%’;
改編碼 ?set ?名稱=gbk;當前窗口有效
character_set_client
character_set_results
在總配置my.ini文件進行配置,一直有效
數據庫備份:(備份數據庫內容,不是備份數據庫)
? ? myslqdump ?-u用戶名 ?-p密碼 ?數據庫名>生成腳本文件路徑
恢復數據:
? ? myslq ?-u用戶名 ?-p密碼 ?數據庫名<生成腳本文件路徑
? ? source ?路徑
退出服務器:exit 或 quit
數據庫語句
查看數據庫: SHOW DATABASES
切換(選擇要操作的)數據庫: USE 數據庫名
? ? ? ? ?創建數據庫: CREATE DATABASE 數據庫名
刪除數據庫:DROP DATABASE 數據庫名
修改數據庫編碼:ALTER DATABASE 數據庫名 ?CHARACTER SET utf8
進入數據庫: USE 數據庫名
創建表
CREATE TABLE 表名(
列名 ?列類型,PRIMARY KEY (主鍵) ?AUTO_INCREMENT (主鍵自增長)
列名 ?列類型, NOT NULL(非空) UNIQUE ?(唯一)
…
列名 ?列類型
);
查看表:SHOW ?TABLES;
查看表結構: ?DESC 表名;
刪除表: ?DROP TABLE 表名;
刪除主鍵:ALTER TABLE 表名 ?DROP PRIMARY KEY
修改表:
添加列:
ALTER ?TABLE ?表名
ADD (
列名 ?列類型,
列名 ?列類型,
…
) ;
修改列類型:
ALTER TABLE 表名
MODIFY 列名 ?新類型;
刪除列:
ALTER TABLE 表名
DROP 列名;
修改表名稱:
ALTER TABLE 表名
RENAME ?TO 新表名;
ALTER ?TABLE ?表名
ADD —>添加列
MODIFY —>修改列名和列類型
CHANGE ?—>修改列名
DROP —>刪除列
RENAME ?TO —>修改表名
數據庫操作語言
?在數據庫中所以的字符串類型,必須使用單引,不能使用雙引
? ? 查詢表記錄:SELECT ?* ?FROM ?表名
? ? 插入數據:
? ? ? ? ? ? ? INSERT ?INTO ?表名 ( 列名1,列名2,列名3,…)VALUES ( 列值1,列值2,列值3,…) ;
? ? 修改數據:
? ? ? ? ?條件運算符:
? ? ? ? ? ? ? =、!=、<>, >, <, >=, <=, BETWEEN…AND, IN(…), IS NULL, NOT, OR, AND
? ? ? ? ?改變一個
? ? ? ? ? ? ? UPDATE ?表名 ?SET ?列名= ‘ 列值 ’ ?WHERE 列名= ‘ 列值 ’
? ? ? ? ?改變多個
? ? ? ? ? ? ? UPDATE ?表名 ?SET ?列名= ‘ 列值 ’ ?WHERE 列名= ‘ 列值 ’ OR 列名= ‘ 列值 ’
? ? ?刪除數據:
? ? ? ? ? DELETE ?FROM ?表名 ?WHERE ?條件;
? ? ? ? ? DELETE ?FROM ?表名 ? (刪除全部數據)
? ? 查詢表記錄:
? ? ? ? ?SELECT ?* ?FROM ?表名
? ? ? ? ?SELECT ?列1,列2... ? FROM ?表名
? ? ? ? ?去除完全相同的重復行
? ? ? ? ?SELECT ?DISTINCT ?列名 ?FROM ?表名
? ? 模糊查詢 ?關鍵字 ?LIKE
? ? ? ? ?SELECT ?* ?FROM ?表名 WHERE ?列名 LIKE ‘_%_'
? ? ? ? ?select ?列
? ? ? ? ?from ? ? 表名
? ? ? ? ?where ? ? 條件
? ? ? ? ?group by ? ? 組
? ? ? ? ?having ? ? 組條件
? ? ? ? ?order ?by ? 列名 ?排序(DESC降序)(ASC升序)
? ? ? ? ?AVG平均值
? ? ? ? ?COUNT(*)數量
* 1.創建數據表
? ?create table userinfo(id integer primary key autoincrement, username varchar(20), password varchar(20))
* 2.插入數據
? ? ? ?insert into userinfo(username, password) values ("lisi","abcd")
* 3.刪除
? ? ? ?delete from userinfo ?刪除表中所有數據
? ? ? ?delete from userinfo where id=4
? ? ? ?delete from userinfo where username='lisi' or password='abcde'
* 4.修改
? ? ? ?update userinfo set password="1234",username='zhangfei' where id=8
* 5.查詢
? ? ? ?select * from userinfo;
? ? ? ?select * from userinfo where id=9
? ? ? ?select password,id from userinfo where username='zhangsan'
創建數據庫繼承 SQLiteOpenHelper
BlackListOpenHelper helper= new BlackListOpenHelper(context);
? ? 獲取自己創建的數據庫的db
SQLiteDatabase db = helper.getWritableDatabase();
獲取已有的數據庫的db
//1.數據庫路徑 ?2. ?3.模式讀寫
SQLiteDatabase db=SQLiteDatabase.openDatabase(file.getAbsolutePath(), null, SQLiteDatabase.OPEN_READONLY);
/**
* 騷擾攔截的數據庫CDUQ
*
* @author Administrator
*
*/
public class BlackListDao {
? ? BlackListOpenHelper helper;
? ? public BlackListDao(Context context) {
? ? ? ? ? super();
? ? ? ? ? helper = new BlackListOpenHelper(context);
? ? }
? ? /**
? ? ?* 數據庫添加
? ? ?*
? ? ?* @param number
? ? ?* @param type
? ? ?* @return
? ? ?*/
? ? public long insert(String number, int type) {
? ? ? ? ? SQLiteDatabase db = helper.getWritableDatabase();
? ? ? ? ? ContentValues values = new ContentValues();
? ? ? ? ? values.put(BlackListDB.TableBlackList.COLUMN_NUMBER, number);
? ? ? ? ? values.put(BlackListDB.TableBlackList.COLUMN_TYPE, type);
? ? ? ? ? long insert = db.insert(BlackListDB.TableBlackList.TABLE_NAME, null,
? ? ? ? ? ? ? ? ? ? values);
? ? ? ? ? db.close();
? ? ? ? ? return insert;
? ? }
? ? /**
? ? ?* 數據庫的刪除
? ? ?*
? ? ?* @param number
? ? ?* @return
? ? ?*/
? ? public int delete(String number) {
? ? ? ? ? SQLiteDatabase db = helper.getWritableDatabase();
? ? ? ? ? int delete = db.delete(BlackListDB.TableBlackList.TABLE_NAME,
? ? ? ? ? ? ? ? ? ? BlackListDB.TableBlackList.COLUMN_NUMBER + "=?",
? ? ? ? ? ? ? ? ? ? new String[] { number });
? ? ? ? ? db.close();
? ? ? ? ? return delete;
? ? }
? ? /**
? ? ?* 數據庫的修改
? ? ?*
? ? ?* @param number
? ? ?* @param type
? ? ?* @return
? ? ?*/
? ? public int update(String number, int type) {
? ? ? ? ? SQLiteDatabase db = helper.getWritableDatabase();
? ? ? ? ? ContentValues values = new ContentValues();
? ? ? ? ? values.put(BlackListDB.TableBlackList.COLUMN_TYPE, type);
? ? ? ? ? int update = db.update(BlackListDB.TableBlackList.TABLE_NAME, values,
? ? ? ? ? ? ? ? ? ? BlackListDB.TableBlackList.COLUMN_NUMBER + "=?",
? ? ? ? ? ? ? ? ? ? new String[] { number });
? ? ? ? ? db.close();
? ? ? ? ? return update;
? ? }
? ? /**
? ? ?* 數據庫的查詢
? ? ?*
? ? ?* @return
? ? ?*/
? ? public List query() {
? ? ? ? ? List list = new ArrayList();
? ? ? ? ? SQLiteDatabase db = helper.getReadableDatabase();
? ? ? ? ? Cursor cursor = db.query(BlackListDB.TableBlackList.TABLE_NAME, null,
? ? ? ? ? ? ? ? ? ? null, null, null, null, null);
? ? ? ? ? while (cursor.moveToNext()) {
? ? ? ? ? ? ? ?String number = cursor.getString(cursor
? ? ? ? ? ? ? ? ? ? ? ? ?.getColumnIndex(BlackListDB.TableBlackList.COLUMN_NUMBER));
? ? ? ? ? ? ? ?int type = cursor.getInt(cursor
? ? ? ? ? ? ? ? ? ? ? ? ?.getColumnIndex(BlackListDB.TableBlackList.COLUMN_TYPE));
? ? ? ? ? ? ? ?BlackListBean blackListBean = new BlackListBean(number, type);
? ? ? ? ? ? ? ?list.add(blackListBean);
? ? ? ? ? }
? ? ? ? ? cursor.close();
? ? ? ? ? db.close();
? ? ? ? ? return list;
? ? }
? ? /**
? ? ?* 數據庫的分頁查詢
? ? ?*
? ? ?* @return
? ? ?*/
? ? public List query(int limit, int offset) {
? ? ? ? ? List list = new ArrayList();
? ? ? ? ? SQLiteDatabase db = helper.getReadableDatabase();
? ? ? ? ? String sql = "select * from " + BlackListDB.TableBlackList.TABLE_NAME
? ? ? ? ? ? ? ? ? ? + " limit ? offset ?";
? ? ? ? ? Cursor cursor = db.rawQuery(sql,
? ? ? ? ? ? ? ? ? ? new String[] { limit + "", offset + "" });
? ? ? ? ? // Cursor cursor = db.query(BlackListDB.TableBlackList.TABLE_NAME, null,
? ? ? ? ? // null, null, null, null, null);
? ? ? ? ? while (cursor.moveToNext()) {
? ? ? ? ? ? ? ?String number = cursor.getString(cursor
? ? ? ? ? ? ? ? ? ? ? ? ?.getColumnIndex(BlackListDB.TableBlackList.COLUMN_NUMBER));
? ? ? ? ? ? ? ?int type = cursor.getInt(cursor
? ? ? ? ? ? ? ? ? ? ? ? ?.getColumnIndex(BlackListDB.TableBlackList.COLUMN_TYPE));
? ? ? ? ? ? ? ?BlackListBean blackListBean = new BlackListBean(number, type);
? ? ? ? ? ? ? ?list.add(blackListBean);
? ? ? ? ? }
? ? ? ? ? cursor.close();
? ? ? ? ? db.close();
? ? ? ? ? return list;
? ? }
? ? /**
? ? ?* 判斷短信是否攔截
? ? ?*
? ? ?* @param number
? ? ?* @return
? ? ?*/
? ? public boolean query(String number) {
? ? ? ? ? SQLiteDatabase db = helper.getReadableDatabase();
? ? ? ? ? String[] projection = { BlackListDB.TableBlackList.COLUMN_TYPE };
? ? ? ? ? Cursor cursor = db.query(BlackListDB.TableBlackList.TABLE_NAME,
? ? ? ? ? ? ? ? ? ? projection, BlackListDB.TableBlackList.COLUMN_NUMBER + "=?",
? ? ? ? ? ? ? ? ? ? new String[] { number }, null, null, null);
? ? ? ? ? try {
? ? ? ? ? ? ? ?if (cursor.moveToNext()) {
? ? ? ? ? ? ? ? ? ? int type = cursor
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?.getInt(cursor
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?.getColumnIndex(BlackListDB.TableBlackList.COLUMN_TYPE));
? ? ? ? ? ? ? ? ? ? return type != 0;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?return false;
? ? ? ? ? } finally {
? ? ? ? ? ? ? ?cursor.close();
? ? ? ? ? ? ? ?db.close();
? ? ? ? ? }
? ? }
? ? /**
? ? ?* 判斷電話是否攔截
? ? ?*
? ? ?* @param number
? ? ?* @return
? ? ?*/
? ? public boolean queryPhone(String number) {
? ? ? ? ? SQLiteDatabase db = helper.getReadableDatabase();
? ? ? ? ? String[] projection = { BlackListDB.TableBlackList.COLUMN_TYPE };
? ? ? ? ? Cursor cursor = db.query(BlackListDB.TableBlackList.TABLE_NAME,
? ? ? ? ? ? ? ? ? ? projection, BlackListDB.TableBlackList.COLUMN_NUMBER + "=?",
? ? ? ? ? ? ? ? ? ? new String[] { number }, null, null, null);
? ? ? ? ? try {
? ? ? ? ? ? ? ?if (cursor.moveToNext()) {
? ? ? ? ? ? ? ? ? ? int type = cursor
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?.getInt(cursor
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?.getColumnIndex(BlackListDB.TableBlackList.COLUMN_TYPE));
? ? ? ? ? ? ? ? ? ? return type != 1;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?return false;
? ? ? ? ? } finally {
? ? ? ? ? ? ? ?cursor.close();
? ? ? ? ? ? ? ?db.close();
? ? ? ? ? }
? ? }
}
分頁數據庫的創建
public interface BlackListDB {
? ? /**
? ? ?* 數據庫的名稱
? ? ?*/
? ? String DB_NAME = "black_list.db";
? ? /**
? ? ?* 數據庫版本
? ? ?*/
? ? int DB_VERSION=1;
? ? public interface TableBlackList{
? ? ? ? ? String TABLE_NAME="blacklist";
? ? ? ? ? String COLUMN_ID = "_id";
? ? ? ? ? String COLUMN_NUMBER = "number";
? ? ? ? ? String COLUMN_TYPE = "type";
? ? ? ? ? String TABLE_SQL="CREATE TABLE "+TABLE_NAME+" ( "
? ? ? ? ? +COLUMN_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "
? ? ? ? ? +COLUMN_NUMBER+" VARCHAR ?UNIQUE, "
? ? ? ? ? +COLUMN_TYPE+" INTEGER)";
? ? }
}
分頁數據庫的查詢
? ? public List query(int limit, int offset) {
? ? ? ? ? List list = new ArrayList();
? ? ? ? ? SQLiteDatabase db = helper.getReadableDatabase();
? ? ? ? ? String sql = "select * from " + BlackListDB.TableBlackList.TABLE_NAME
? ? ? ? ? ? ? ? ? ? + " limit ? offset ?";
? ? ? ? ? Cursor cursor = db.rawQuery(sql,
? ? ? ? ? ? ? ? ? ? new String[] { limit + "", offset + "" });
? ? ? ? ? // Cursor cursor = db.query(BlackListDB.TableBlackList.TABLE_NAME, null,
? ? ? ? ? // null, null, null, null, null);
? ? ? ? ? while (cursor.moveToNext()) {
? ? ? ? ? ? ? ?String number = cursor.getString(cursor
? ? ? ? ? ? ? ? ? ? ? ? ?.getColumnIndex(BlackListDB.TableBlackList.COLUMN_NUMBER));
? ? ? ? ? ? ? ?int type = cursor.getInt(cursor
? ? ? ? ? ? ? ? ? ? ? ? ?.getColumnIndex(BlackListDB.TableBlackList.COLUMN_TYPE));
? ? ? ? ? ? ? ?BlackListBean blackListBean = new BlackListBean(number, type);
? ? ? ? ? ? ? ?list.add(blackListBean);
? ? ? ? ? }
? ? ? ? ? cursor.close();
? ? ? ? ? db.close();
? ? ? ? ? return list;