简体   繁体   中英

Writing to the database from a file

My program backs up specific data at a specific time interval. I want to make it possible to restore. The problem is that I don't know how to load data from a file. Inside the file, the data is separated by ; . The recording goes to a file with the extension .txt

A snippet of code how data is written to a file:

ContentValues cv = new ContentValues();

        cv.put("DlvDate", DlvDate);
        cv.put("PayDate", PayDate);
        cv.put("DocType", DocType);
        cv.put("Cust", Cust);
        cv.put("PaymMode", PaymMode);
        cv.put("PaymTerm", PaymTerm);
        cv.put("PriceGroup", PriceGroup);
        cv.put("CreatedDateTime", CreatedDateTime);
        cv.put("Note", Note);
        if (_id != 0) {
            GlobalVars.db.update("SalesTable", cv, "_id = ?", new String[]{String.valueOf(_id)});
        } else {
            _id = GlobalVars.db.insert("SalesTable", null, cv);
        }

        File sFile = new File(GlobalVars.ArchiveDir + "/" + String.valueOf(_id) + ".txt");
        BufferedWriter br;
        try {
            br = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(sFile), "windows-1251"));
            br.write(this.salesStr() + "\r\n");
            br.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
String sLine;
        CustTable ct = new CustTable(this.Cust);
        Cursor cSalesLine = GlobalVars.db.rawQuery("select * from SalesLine where SalesId = ?",
                                                   new String[]{String.valueOf(this._id)});
        sLine = this.Cust                         //1
                + ";"
                + this.DocType                    //2
                + ";"
                + ct.PaymTerm                     //3
                + ";"
                + ct.PaymMode                     //4
                + ";"
                + GlobalVars.dateSQLtoLocal(this.DlvDate) //5
                + ";"
                + this.SalesTime                             //6
                + ";"
                + GlobalVars.dateSQLtoLocal(this.PayDate)    //7
                + ";"
                + ct.PriceGroup                              //8
                + ";"
                + this.Note                                  //9
                + ";;"
                + this.CreatedDateTime                       //11
                + ";"
                + this.CustOrderNo
                + "&"
                + ";";
        cSalesLine.moveToFirst();
        while (!cSalesLine.isAfterLast()) {
            sLine += cSalesLine.getString(cSalesLine.getColumnIndex("Invent"))
                    + ";"
                    + cSalesLine.getString(cSalesLine.getColumnIndex("Qty"))
                    + ";"
                    + ";";
            cSalesLine.moveToNext();
        }
        return sLine.substring(0, sLine.length() - 1);

If you take the answer from the link I gave you by R9J (The accepted answer) You can see he's reading the file line by line:

while ( (receiveString = bufferedReader.readLine()) != null ) {
                stringBuilder.append("\n").append(receiveString);
            }

In your case, you can replace the stringBuilder.append part with

String str = receiveString;

From that point, you can split your str using ; and you'll get a string array with each cell containing a different value, as follow:

String[] rowVals = str.split(";");

I'm not 100% sure if ';' is a valid char on it's own, if not, you can use

String[] rowVals = str.split("\;");

At this point, rowVals[0] will be the first value inserted in that line (Cust, if I read your code correctly) and rowVals[1] will be DocType and so on. I do suggest checking the values before applying them (for example, rowVals[i].length > 0 or something of the sort)

Thanks to Dan Baruch, I was able to figure it out in more detail and do what I needed.

Here's what I ended up with:

try(BufferedReader br = new BufferedReader(new InputStreamReader( new FileInputStream(fileS), "windows-1251"))) {
                            StringBuilder sb = new StringBuilder();

                            String line = br.readLine();

                            while (line != null) {
                                sb.append(line);
                                sb.append(System.lineSeparator());
                                line = br.readLine();
                            if ( line != null) {
                                String[] rowVals = line.split(";", 11);
                                Cust = rowVals[0];
                                DocType = rowVals[1];
                                PaymTerm = rowVals[2];
                                PaymMode = rowVals[3];
                                DlvDate = rowVals[4];
                                SalesTime = rowVals[5];
                                PayDate = rowVals[6];
                                PriceGroup = rowVals[7];
                                Note = rowVals[8];
                                CreatedDateTime = rowVals[9];
                                CustOrderNo = rowVals[10];
                                Log.e("loginA", rowVals.toString());
                            }else{
                                br.close();
                                line = sb.toString();
                            }
                            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM