数据库插入数据

时间:2015-01-25 11:31:16

标签: android mysql sqlite

我在Web服务中接收数据并在sqlite数据库中插入Android但是如果字段为空则在提交之前重复为新提交的文件没有空并重复... 请帮帮我:

public class gfc extends AsyncTask {

    private String Link = "";
    private String Count = "";
    private String tid = "";
    private String tim = "";
    private String tuser = "";
    private String tusername = "";
    private String tmatn = "";
    private String tcomments = "";
    private String tlikes = "";
    private String tdate = "";
    private String tip_addr = "";
    private database db;

    public gfc(String link, String count, Context c) {
        Link = link;
        Count = count;
        db = new database(c);
    }

    @Override
    protected String doInBackground(Object... arg0) {

        try {
            String data = URLEncoder.encode("count", "UTF8") + "=" + URLEncoder.encode(Count, "UTF8");

            URL mylink = new URL(Link);
            URLConnection connect = mylink.openConnection();

            connect.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(connect.getOutputStream());
            wr.write(data);
            wr.flush();

            BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream()));
            StringBuilder sb = new StringBuilder();

            String line = null;
            db.open();

            while ((line = reader.readLine()) != null) {
                int f = 0;
                int c = 0;

                for (int i = 0; i < line.length(); i++) {
                    if (line.charAt(i) == '|') {
                        String temp = line.substring(f, i);
                        if (c == 0) {
                            tid = temp;
                        }
                        if (c == 1) {
                            tdate = temp;
                        }
                        if (c == 2) {
                            tuser = temp;
                        }
                        if (c == 3) {
                            tusername = temp;
                        }
                        if (c == 4) {
                            tip_addr = temp;
                        }
                        if (c == 5) {
                            tcomments = temp;
                        }
                        if (c == 6) {
                            tlikes = temp;
                        }
                        if (c == 7) {
                            tim = temp;
                        }
                        if (c == 8) {
                            tmatn = temp.replace("^", "\n");
                        }

                        c += 1;
                        f = i + 1;
                    }
                }

                db.insert(tid, tuser, tusername, tmatn, tcomments, tlikes, tdate, tip_addr, tim);
                sb.append("t");
            }

            db.close();
            index.res = sb.toString();

        } catch (Exception e) {}

        return "";
    }
}

0 个答案:

没有答案