MySQL如何使用" - "在它的名字?

时间:2017-06-11 12:29:55

标签: mysql sql database

我正在使用MySQL for Windows 7.我有一个列有一个" - " (减号)的名称。不知怎的,我无法运行以下命令:

INSERT INTO table (..., var-name, ...) VALUES(..., value, ...);

有人可以帮助我如何执行此命令吗?

使用

INSERT INTO table (..., [var-name], ...) VALUES(..., value, ...);

无效

3 个答案:

答案 0 :(得分:0)

你必须用反引号(`)包装名称,如下所示:

private void selectImage(){

    final CharSequence[] item={"Camera","Document","Cancel"};

    AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Carica");
    builder.setItems(item, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int i) {
            if(item[i].equals("Camera")){
                Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent,Request_camera);
            }
            else if(item[i].equals("Document")){

                Intent intent3 = new Intent();
                intent3.setType("*/*");
                intent3.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent3,"Choose File to Upload.."),PICK_FILE_REQUEST);
                Uri.parse(Environment.getExternalStorageDirectory().toString());


            }

            else if(item[i].equals("Cancel")){
                dialog.dismiss();
            }
        }
    }).show();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode,resultCode,data);

    if(resultCode== Activity.RESULT_OK){
        if(requestCode==Request_camera && button1==1){

            Bundle bundle=data.getExtras();
            final Bitmap bpm= (Bitmap)bundle.get("data");
            uploadImage.setImageBitmap(bpm);
            bitmapToBase64(bpm);

            String mynewString=bitmapToBase64(bpm);
            mydocument.set(0,mynewString);
            decode64.setText(mynewString);
            getFileExt(uploadImage.toString());

        }
         else if(requestCode==Request_camera && button1==2){
            Bundle bundle=data.getExtras();
            final Bitmap bpm= (Bitmap)bundle.get("data");
            imageView2.setImageBitmap(bpm);
            bitmapToBase64(bpm);

            String mynewString=bitmapToBase64(bpm);
            decodee2.setText(mynewString);
            decodee2.setTextColor(Color.GREEN);
        }

        else if(requestCode==Request_file){
            Uri selectImageUri=data.getData();
            uploadImage.setImageURI(selectImageUri);
            String newNome=getFileName(selectImageUri);
            decode64.setText(newNome);

            String realPath=selectImageUri.toString();

            decodee2.setText(realPath);

            File file=new File(newNome);

            getStringFile(file);
            String myDecode=getStringFile(file);
            mydocument.set(0,myDecode);


        }
    }

}


private String bitmapToBase64(Bitmap bitmap) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream .toByteArray();
    return Base64.encodeToString(byteArray, Base64.DEFAULT);
}

为了逃避破折号。

答案 1 :(得分:0)

列名称的MySQL转义字符不是INSERT INTO table (..., `var-name`, ...) VALUES(..., value, ...); ,它是[

所以你需要使用:

`

答案 2 :(得分:0)

等待发生的事故......

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table (plusses INT NOT NULL, minuses INT NOT NULL, `plusses-minuses` INT NOT NULL);

INSERT INTO my_table VALUES
(10,2,6),
(12,6,6),
(13,9,6),
(14,12,6),
(15,2,6);

SELECT * FROM my_table;
+---------+---------+-----------------+
| plusses | minuses | plusses-minuses |
+---------+---------+-----------------+
|      10 |       2 |               6 |
|      12 |       6 |               6 |
|      13 |       9 |               6 |
|      14 |      12 |               6 |
|      15 |       2 |               6 |
+---------+---------+-----------------+

SELECT plusses, minuses, plusses-minuses FROM my_table;
+---------+---------+-----------------+
| plusses | minuses | plusses-minuses |
+---------+---------+-----------------+
|      10 |       2 |               8 |
|      12 |       6 |               6 |
|      13 |       9 |               4 |
|      14 |      12 |               2 |
|      15 |       2 |              13 |
+---------+---------+-----------------+