Mariadb 加载本地 infile 并引用一些字段

问题描述

我在任何地方都找不到这个特定的问题...

我有许多 CSV 文件要导入 MariaDB。某些字段(并非所有字段)周围有双引号,因为它们包含逗号。这是一个例子:

public class MainActivity extends AppCompatActivity {
private static final int LOCATION = 1;
Button btn1,btn2;
String command;
String networkSSID = "Esp8266TestNet";
String networkPass = "Esp8266Test";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn1=findViewById(R.id.btn1);
    btn2=findViewById(R.id.btn2);
    //------------------------------------------------------
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            command="/led/on\r";
            send_request(command);
        }
    });
    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            command="/led/off\r";
            send_request(command);
        }
    });
}
public void send_request(String message) {
    send_request sr = new send_request();
    sr.execute(message);
}
class send_request extends AsyncTask<String,Void,String> {

    @Override
    protected String doInBackground(String... message) {
        try {
            /*note : ip address must be in Quotation mark*/
            /*note : port doesn't need to be inside the Quotation mark*/
            Socket s = new Socket(/*ip address :*/"192.168.4.1",/*port :*/ 80);
            PrintWriter printWriter = new PrintWriter(s.getOutputStream());
            printWriter.write(message[0]);
            printWriter.flush();
            printWriter.close();
            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void onPostExecute(final String Data) {
        /*Data is what you receive from your server*/
        Log.e("my_Data","Data is : " + Data);
        Toast.makeText(MainActivity.this,Data,Toast.LENGTH_SHORT).show();
    }
}

protected void onStart() {
    super.onStart();
    //Assume you want to read the SSID when the activity is started
    tryToReadSSID();
}

@Override
public void onRequestPermissionsResult(int requestCode,String[] permissions,int[] grantResults) {
    if(grantResults[0] == PackageManager.PERMISSION_GRANTED && requestCode == LOCATION){
        //User allowed the location and you can read it now
        tryToReadSSID();
    }
}

private String tryToReadSSID() {
    String ssid="no ssid";
    //If requested permission isn't Granted yet
    if (ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        //Request permission from user
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},LOCATION);
    }else{//Permission already granted
        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        if(wifiInfo.getSupplicantState() == SupplicantState.COMPLETED){
            ssid = wifiInfo.getSSID();//Here you can access your SSID
        }
    }
    return ssid;
}

我的字段条件是:

1,2,No Comma
2,3,"Has,a,comma"
3,4,No comma here

当我导入文件时,它会加载,直到它到达带有带引号的字段的第一行。然后,它退出。如果我手动删除该字段上的引号(和嵌入的逗号),它将加载,直到遇到下一个带引号的字段,然后停止。

我相信它加载了第一行并且没有看到引号,因此它假定没有引号。然后,当它看到引用时,它会停止。那么,我如何告诉它在某些行上引用了某些字段,而不是在所有行上引用了所有字段?

更新:作为一种变通方法,我编写了一个 Perl 脚本,强制在每一行的每个字段周围加上引号。我不想要额外的步骤,也不想以任何方式更改原始文件,但这是我现在必须做的。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)