分类向量存储在引号中时的RSQLite查询

时间:2017-05-12 21:22:38

标签: mysql r sqlite rsqlite

我有一个SQLite数据库,我使用R的clickPoint包查询。我有一个分类矢量我想过滤我的查询,以便我的查询看起来像这样:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SelectionExample {

    public static void main(String[] args) {
        new SelectionExample();
    }

    public SelectionExample() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private Point clickPoint;

        private Shape shape;
        private Rectangle box;

        public TestPane() {
            MouseAdapter ma = new MouseAdapter() {

                @Override
                public void mouseDragged(MouseEvent e) {
                    int minX = Math.min(e.getX(), clickPoint.x);
                    int minY = Math.min(e.getY(), clickPoint.y);
                    int maxX = Math.max(e.getX(), clickPoint.x);
                    int maxY = Math.max(e.getY(), clickPoint.y);

                    box = new Rectangle(minX, minY, maxX - minX, maxY - minY);
                    int size = Math.min(maxX - minX, maxY - minY);
                    if (minX < clickPoint.x) {
                        minX = clickPoint.x - size;
                    }
                    if (minY < clickPoint.y) {
                        minY = clickPoint.y - size;
                    }

                    shape = new Ellipse2D.Double(minX, minY, size, size);
                    repaint();
                }

                @Override
                public void mousePressed(MouseEvent e) {
                    clickPoint = new Point(e.getPoint());
                }

            };

            addMouseListener(ma);
            addMouseMotionListener(ma);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (shape != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setColor(new Color(0, 0, 255, 64));
                g2d.fill(shape);
                g2d.setColor(Color.BLUE);
                g2d.draw(shape);
                g2d.draw(box);
                g2d.dispose();
            }
        }
    }

}

这通常可以正常工作,并返回RSQLite级别为dbGetQuery(mydb, 'select PLT_CN, INVYR from GRM where ESTN_TYPE = "AL"') 的所有数据。

但是。

它没有这样做。这是因为在存储数据的ESTN_TYPE文件中,值AL实际上输入为.csv。因此,当我查询AL时,我的查询返回零数据。我该如何解决这个问题?

(感谢@Parfait让我意识到这是我之前提出的问题中的真正问题)。

1 个答案:

答案 0 :(得分:3)

RSQLite

library(DBI)
con <- dbConnect(RSQLite::SQLite(), ":memory:")
df <- data.frame(a=1:5, b=sprintf('"%s"', letters[1:5]), stringsAsFactors=F)
df
#   a   b
# 1 1 "a"
# 2 2 "b"
# 3 3 "c"
# 4 4 "d"
# 5 5 "e"
dbWriteTable(con, "tbl", df)
# [1] TRUE
dbGetQuery(con, 'select * from tbl')
#   a   b
# 1 1 "a"
# 2 2 "b"
# 3 3 "c"
# 4 4 "d"
# 5 5 "e"
dbGetQuery(con, 'select * from tbl where b="a"')
# [1] a b
# <0 rows> (or 0-length row.names)

无论如何,使用参数化查询通常是件好事,所以可以说是双鸟一石:

dbGetQuery(con, 'select * from tbl where b=:x', params=list(x='"a"'))
#   a   b
# 1 1 "a"
dbGetQuery(con, 'select * from tbl where b in (:x)', params=list(x=c('"a"','"c"')))
#   a   b
# 1 1 "a"
# 2 3 "c"

RMySQL

(我没有mysql的实例,所以这是猜测。)

使用@x代替:x

dbGetQuery(con, 'select * from tbl where b=@x', params=list(x='"a"'))