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;