数据库

来源:互联网 发布:迪杰斯特拉算法原理 编辑:程序博客网 时间:2024/06/17 17:08

创建数据库

>sqlite数据库 轻量级嵌入式的数据库

//1.声明数据库的文件类MyDBOpenHelper helper = new MyDBOpenHelper(this);//2.关键代码,必须通过帮助类 获取一个数据库文件  数据库文件才被创建出来helper.getWritableDatabase();
/** * 数据库创建的帮助类 类似文件File类 */public class MyDBOpenHelper extends SQLiteOpenHelper {public MyDBOpenHelper(Context context) {//第二个参数:数据库文件的名称//第三个参数: null 代表的是默认的游标(索引)工厂(控制索引)//第四个参数:是数据库的版本号  数据库只能升级,不能降级,版本号只能变大不能变小super(context, "test.db", null, 1); }//Called when the database is created for the first time.//当数据库第一次被创建的时候调用的方法,适合在这个方法里面把数据库的表结构定义出来.@Overridepublic void onCreate(SQLiteDatabase db) { //db:当前的数据库System.out.println("oncreate 数据库被创建了------------");//执行sql语句db.execSQL("create table contactinfo (id integer primary key autoincrement, name varchar(20), phone varchar(20))");//创建联系人的信息表,表名contactinfo //id:integer 整型 primary key 主键 autoincrement 自增长//name:varchar(20)长度为20的字符串}//Called when the database needs to be upgraded.//当数据库更新的时候调用的方法  如果旧的数据库表结构定义的不是特别合理,修改数据库的表结构@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {System.out.println("onUpgrade 数据库被更新了. oldVersion:"+oldVersion+"----newVersion:"+newVersion+"----");db.execSQL("alter table contactinfo add account varchar(20)");//修改表  增加一列}}

增删改查的sql语句1.增insert into contactinfo (name,phone) values(‘zhangsan’,‘110’)//添加一条记录  姓名zhangsan 电话1102.删delete from contactinfo where id=‘1’3.改update contactinfo set phone=‘119’where name=‘zhangsan’//修改zhangsan的电话号码为1194.查select phone from contactinfo where name=‘zhangsan’//查询zhangsna的电话号码

/** * 联系人数据库表的访问类  Dao 数据的访问存储 */public class ContactInfoDao {/** * 数据库打开的帮助类 */private MyDBOpenHelper helper;/** * 在构造方法里面完成 必须要用的类的初始化 * @param context */public ContactInfoDao(Context context) {helper = new MyDBOpenHelper(context);}/** * 添加一条记录 * @param name 联系人姓名 * @param phone 联系人电话 */public void add(String name, String phone){SQLiteDatabase db = helper.getWritableDatabase();db.execSQL("insert into contactinfo (name,phone) values (?,?)", new Object[]{name,phone});// ? 占位符//记得释放数据库资源db.close();}/** * 根据姓名删除一条记录 * @param name 要删除的联系人的姓名 */public void delete(String name){//判断这个数据是否存在.SQLiteDatabase db = helper.getWritableDatabase();db.execSQL("delete from contactinfo where name=?", new Object[]{name});db.close();//再从数据库里面查询一遍,看name是否还在}/** * 修改联系人电话号码 * @param newphone 新的电话号码 * @param name 要修改的联系人姓名 */public void update(String newphone , String name){SQLiteDatabase db = helper.getWritableDatabase();db.execSQL("update contactinfo set phone =? where name=?", new Object[]{newphone,name});db.close();}/** * 查询联系人的电话号码 * @param name 要查询的联系人 * @return 电话号码 */public String getPhoneNumber(String name){String phone = null;SQLiteDatabase db = helper.getReadableDatabase();Cursor  cursor = db.rawQuery("select phone from contactinfo where name=?", new String[]{name});//cursor 指向数据库的第0行 id name phone 等头if(cursor.moveToNext()){//如果光标可以移动到下一位,代表就是查询到了数据phone = cursor.getString(0); //假如第0列是phone}cursor.close();//关闭掉游标,释放资源db.close();//关闭数据库,释放资源return phone;}}

private ContactInfoDao dao;dao = new ContactInfoDao(this);//添加一条联系人的信息dao.add("aaa", "123");//删除一条记录dao.delete("aaa");//修改联系人的号码dao.update("bbb", "321");//查询String phone = dao.getPhoneNumber("bbb");
--------------------------------------------------------------------
/** * 联系人数据库表的访问类  Dao 数据的访问存储 */public class ContactInfoDao {/** * 数据库打开的帮助类 */private MyDBOpenHelper helper;/** * 在构造方法里面完成 必须要用的类的初始化 * @param context */public ContactInfoDao(Context context) {helper = new MyDBOpenHelper(context);}/** * 添加一条记录 * @param name 联系人姓名 * @param phone 联系人电话 * @return 返回的是添加后在数据库的行号  返回-1代表添加失败 */public long add(String name, String phone){SQLiteDatabase db = helper.getWritableDatabase();/**db.execSQL("insert into contactinfo (name,phone) values (?,?)", new Object[]{name,phone});// ? 占位符*/ContentValues values = new ContentValues();values.put("name",name);values.put("phone",phone);//第一个参数:表名   values:要添加的数据     内部是通过组拼sql语句实现的long rowid = db.insert("contactinfo",null,values);//记得释放数据库资源db.close();return rowid;}/** * 根据姓名删除一条记录 * @param name 要删除的联系人的姓名 * @return 返回0代表的是没有删除任何的记录  返回正数int值代表删除了几条数据 */public int delete(String name){//判断这个数据是否存在.SQLiteDatabase db = helper.getWritableDatabase();/**db.execSQL("delete from contactinfo where name=?", new Object[]{name});*///第一个参数:表名   第二个参数:条件 第三个参数:具体的条件int rowcount= db.delete("contactinfo","name=?",new String[]{name});db.close();return rowcount;}/** * 修改联系人电话号码 * @param newphone 新的电话号码 * @param name 要修改的联系人姓名 * @return 返回0代表的是一行也没有更新成功  返回正数int值代表更新了几行记录 */public int update(String newphone , String name){SQLiteDatabase db = helper.getWritableDatabase();/**db.execSQL("update contactinfo set phone =? where name=?", new Object[]{newphone,name});*///第二个参数:要修改的数据ContentValues values = new ContentValues();values.put("phone",newphone);db.update("contactinfo",values,"name=?",new String[]{name});db.close();return rowcount;}/** * 查询联系人的电话号码 * @param name 要查询的联系人 * @return 电话号码 */public String getPhoneNumber(String name){String phone = null;SQLiteDatabase db = helper.getReadableDatabase();/**Cursor  cursor = db.rawQuery("select phone from contactinfo where name=?", new String[]{name});//cursor 指向数据库的第0行 id name phone 等头*///第二个参数:想获取的哪列的内容Cursor  cursor = db.query("contactinfo", new String[]{"phone"},"name=?",new String[]{name},null,null,null);if(cursor.moveToNext()){//如果光标可以移动到下一位,代表就是查询到了数据phone = cursor.getString(0); //假如第0列是phone}cursor.close();//关闭掉游标,释放资源db.close();//关闭数据库,释放资源return phone;}}

private ContactInfoDao dao;dao = new ContactInfoDao(this);//添加一条联系人的信息long id = dao.add("aaa", "123");if(id==-1){//添加失败}else{//添加成功 id为添加成功后在数据库中的行号}//删除一条记录int count = dao.delete("aaa");//count==0删除失败   否则删除成功 删除了count行//修改联系人的号码int count = dao.update("bbb", "321");//count==0更新失败   否则更新成功 更新了count行记录//查询String phone = dao.getPhoneNumber("bbb");

/** * 查询的逻辑 *  * @param position *            数据在数据库表里面的位置 * @return Map<数据库的列名,值> */public Map<String, String> getStudentInfo(int position) {SQLiteDatabase db = helper.getReadableDatabase();Cursor cursor = db.query("info", new String[] { "studentid", "name","phone" }, null, null, null, null, null);cursor.moveToPosition(position);String studentid = cursor.getString(0);//第0列是studentidString name = cursor.getString(1);//第1列是nameString phone = cursor.getString(2);//第二列是phonecursor.close();db.close();Map<String, String> result = new HashMap<String, String>();result.put("studentid", studentid);result.put("name", name);result.put("phone", phone);return result;}/** * 查询数据库里面一共有多少条记录 */public int getTotalCount() {SQLiteDatabase db = helper.getReadableDatabase();Cursor cursor = db.query("info", null, null, null, null, null, null);int count = cursor.getCount();cursor.close();db.close();return count;}



命令行中
sqlite3 aaa.db
select * from contactinfo;//列出contactinfo表中信息


修改cmd的默认编码
chcp 65001 改成utf-8的编码
chcp 936 改成gbk的编码

原创粉丝点击