zoukankan      html  css  js  c++  java
  • Android笔记之调用

    1、DbOpenHelper.java

    package com.utils;
    
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    
    public class DbOpenHelper extends SQLiteOpenHelper {
    
        private static String dbName = CommonUrl.Db_Name;// 数据库名称
        private static int version = CommonUrl.Db_Version;// 数据库版本号
    
        public DbOpenHelper(Context context) {
            super(context, dbName, null, version);
            // TODO Auto-generated constructor stub
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            // 在数据库中创建表Course
            String sql1 = "create table Course(id integer primary key autoincrement,week text not null,courseindex text not null,coursename text not null,coursetime int not null,room text not null ,teacher text not null,doubleonly int default 0,term int default 0)";
    db.execSQL(sql1); }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } }

    查找

    package dao;
    
    import java.sql.Timestamp;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import service.StudentInfoService;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteConstraintException;
    import android.database.sqlite.SQLiteDatabase;
    import android.util.Log;
    
    import com.utils.CommonUrl;
    import com.utils.DbOpenHelper;
    import com.utils.GsonUlits;
    import com.utils.HttpClientUtils;
    
    public class StudentInfoDao implements StudentInfoService {
    
        private DbOpenHelper helper = null;
    
        public StudentInfoDao(Context context) {
            super();
            helper = new DbOpenHelper(context);
        }
    
        @Override
        public boolean fillStudentInfo(Object[] infolist) {
            // TODO Auto-generated method stub
            boolean flag = false;
            // 实现对数据库的添加,删除和修改功能
            SQLiteDatabase database = null;
            try {
                String sql = "insert into StuInfo(stuid ,name ,sex ,birthday ,xueyuan ,zhuanye, banhao,tel ,qq) values (?,?,?,?,?,?,?,?,?)";
                database = helper.getWritableDatabase();
                database.execSQL(sql, infolist);
                flag = true;
            } catch (SQLiteConstraintException e) {
                // column stuid is not unique (code 19)
                e.printStackTrace();
            }
            if (!flag) {
                String sql = "update StuInfo set name=? ,sex=? ,birthday=? ,xueyuan=? ,zhuanye=?, banhao=? ,tel=?,qq=? where stuid=?";
                database = helper.getWritableDatabase();
                String stuid = (String) infolist[0];
                for (int i = 0; i < 8; i++) {
                    infolist[i] = infolist[i + 1];
                }
                infolist[8] = stuid;
                database.execSQL(sql, infolist);
                flag = true;
            }
            return flag;
        }
    
        @Override
        public Map<String, String> getSimpleInfo(String selection,
                String[] selectionArgs) {
            // TODO Auto-generated method stub
    
            Map<String, String> map = new HashMap<String, String>();
            SQLiteDatabase database = null;
            Cursor cursor = null;// query返回的对象是Cursor
            try {
                database = helper.getWritableDatabase();
                // String[] co = { "coursename", "room", "teacher", "week" };
                // 在2.X版本下query方法汇出错误:
                // java.lang.NoSuchMethodError:
                // android.database.sqlite.SQLiteDatabase.query
                cursor = database.query(true, "StuInfo", null, selection,
                        selectionArgs, null, null, null, null, null);
                int cols_num = cursor.getColumnCount();
                while (cursor.moveToNext()) {
                    for (int i = 0; i < cols_num; i++) {
                        String cols_name = cursor.getColumnName(i);
                        String cols_value = cursor.getString(cursor
                                .getColumnIndex(cols_name));
                        if (cols_value == null) {
                            cols_value = "";
                        }
                        Log.i(i + "", "cols_name=" + cols_name + ",cols_value="
                                + cols_value);
                        map.put(cols_name, cols_value);
                    }
                }
    
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            } finally {
                if (database != null) {
                    database.close();
                }
            }
            return map;
        }
    
        @Override
        public boolean poststudent(Map<String, String> map) {
            // TODO Auto-generated method stub
            Map<String, String> info = new HashMap<String, String>();
            info.put("student.stuid", map.get("stuid"));
            info.put("student.name", map.get("name"));
            info.put("student.sex", map.get("sex"));
            info.put("student.xueyuan", map.get("xueyuan"));
            info.put("student.zhuanye", map.get("zhuanye"));
            info.put("student.banhao", map.get("banhao"));
            info.put("student.birthday", map.get("birthday"));
            String result = HttpClientUtils.sendHttpclientPost(
                    CommonUrl.Add_Student, info, "utf8");
            if (result.contains("成功"))
                return true;
            return false;
        }
    
        @Override
        public List<Map<String, Object>> getstudentlist(String banhao) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("tmp", "");
            // TODO Auto-generated method stub
            String jsonString = HttpClientUtils.sendHttpclientPost(
                    CommonUrl.Student_List, map, CommonUrl.encode);
            if (jsonString.equals(""))
                return null;
            List<Map<String, Object>> list = GsonUlits
                    .getListKeyapFromGson(jsonString);
            return list;
        }
    
        @Override
        public List<Map<String, String>> getMoreInfo(String selection,
                String[] selectionArgs) {
            // TODO Auto-generated method stub
            List<Map<String, String>> list = new ArrayList<Map<String, String>>();
            SQLiteDatabase database = null;
            Cursor cursor = null;// query返回的对象是Cursor
            try {
                database = helper.getWritableDatabase();
    
                cursor = database.query(true, "StuInfo", null, selection,
                        selectionArgs, null, null, null, null, null);
                int cols_num = cursor.getColumnCount();
                while (cursor.moveToNext()) {
                    Map<String, String> map = new HashMap<String, String>();
                    for (int i = 0; i < cols_num; i++) {
                        String cols_name = cursor.getColumnName(i);
                        String cols_value = cursor.getString(cursor
                                .getColumnIndex(cols_name));
                        if (cols_value == null) {
                            cols_value = "";
                        }
                        Log.i(i + "", "cols_name=" + cols_name + ",cols_value="
                                + cols_value);
    
                        map.put(cols_name, cols_value);
                    }
                    list.add(map);
                }
    
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            } finally {
                if (database != null) {
                    database.close();
                }
            }
            return list;
        }
    
        @Override
        public boolean addMessage(String body, String stuid, String type) {
            // TODO Auto-generated method stub
    
            boolean flag = false;
            // 实现对数据库的添加,删除和修改功能
            SQLiteDatabase database = null;
            Timestamp ts = new Timestamp(System.currentTimeMillis());
            Object[] infolist = { stuid, body, ts, type };
            try {
                String sql = "insert into Message(stuid ,body ,timestamp,type) values (?,?,?,?)";
                database = helper.getWritableDatabase();
    
                database.execSQL(sql, infolist);
                flag = true;
            } catch (SQLiteConstraintException e) {
                // column stuid is not unique (code 19)
                e.printStackTrace();
            }
            return flag;
        }
    
        @Override
        public List<Map<String, String>> getMessageList(String stuid) {
            // TODO Auto-generated method stub
            List<Map<String, String>> list = new ArrayList<Map<String, String>>();
            SQLiteDatabase database = null;
            Cursor cursor = null;// query返回的对象是Cursor
            try {
                database = helper.getWritableDatabase();
    
                String selection = "stuid=?";
                String[] selectionArgs = { stuid };
                cursor = database.query(true, "Message", null, selection,
                        selectionArgs, null, null, "timestamp", null, null);
                int cols_num = cursor.getColumnCount();
                while (cursor.moveToNext()) {
                    Map<String, String> map = new HashMap<String, String>();
                    for (int i = 0; i < cols_num; i++) {
                        String cols_name = cursor.getColumnName(i);
                        String cols_value = cursor.getString(cursor
                                .getColumnIndex(cols_name));
                        if (cols_value == null) {
                            cols_value = "";
                        }
    
                        map.put(cols_name, cols_value);
                    }
                    list.add(map);
                }
    
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            } finally {
                if (database != null) {
                    database.close();
                }
            }
            return list;
        }
    }

    Done!

  • 相关阅读:
    Hadoop_HDFS文件读写代码流程解析和副本存放机制
    Hadoop_MapReduce流程
    Hadoop_YARN框架
    Spark任务流程笔记
    3D俄罗斯方块设计
    Hadoop_FileInputFormat分片
    二叉查找树的懒惰删除(lazy deletion)
    数组的三种随机排序方法
    SpringBoot @Async 异步处理业务逻辑和发短信逻辑
    json字符串转java对象
  • 原文地址:https://www.cnblogs.com/xingyyy/p/3496437.html
Copyright © 2011-2022 走看看