예제 #1
0
    def testUnicodeUTF8(self):
        peacesign_unicode = u"\u262e"
        peacesign_utf8 = "\xe2\x98\xae"

        cnn = dbapi.connect(host=DB_HOST,
                            user=DB_USER,
                            password=DB_PASSWD,
                            db=DB_DB,
                            charset='utf-8',
                            use_unicode=True)

        cur = cnn.cursor()
        cur.execute("drop table if exists tblutf")
        cur.execute(
            "create table tblutf (test_id int(11) DEFAULT NULL, test_string VARCHAR(32) DEFAULT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8"
        )

        cur.execute(
            "insert into tblutf (test_id, test_string) values (%s, %s)",
            (1, peacesign_unicode))  # This should be encoded in utf8
        cur.execute(
            "insert into tblutf (test_id, test_string) values (%s, %s)",
            (2, peacesign_utf8))

        cur.execute("select test_id, test_string from tblutf")
        result = cur.fetchall()

        # We expect unicode strings back
        self.assertEquals([(1, peacesign_unicode), (2, peacesign_unicode)],
                          result)
예제 #2
0
    def testBigInt(self):
        """Tests the behaviour of insert/select with bigint/long."""

        BIGNUM = 112233445566778899

        cnn = dbapi.connect(host = DB_HOST, user = DB_USER,
                            password = DB_PASSWD, db = DB_DB,
                            charset = 'latin-1', use_unicode = True)

        cur = cnn.cursor()

        cur.execute("drop table if exists tblbigint")
        cur.execute("""create table tblbigint (
                            test_id int(11) DEFAULT NULL,
                            test_bigint bigint DEFAULT NULL,
                            test_bigint2 bigint DEFAULT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1""")
        cur.execute("insert into tblbigint (test_id, test_bigint, test_bigint2) values (%s, " + str(BIGNUM) + ", %s)", (1, BIGNUM))
        cur.execute(u"insert into tblbigint (test_id, test_bigint, test_bigint2) values (%s, " + str(BIGNUM) + ", %s)", (2, BIGNUM))


        # Make sure both our inserts where correct (ie, the big number was not truncated/modified on insert)
        cur.execute("select test_id from tblbigint where test_bigint = test_bigint2")
        result = cur.fetchall()
        self.assertEquals([(1, ), (2, )], result)


        # Make sure select gets the right values (ie, the big number was not truncated/modified when retrieved)
        cur.execute("select test_id, test_bigint, test_bigint2 from tblbigint where test_bigint = test_bigint2")
        result = cur.fetchall()
        self.assertEquals([(1, BIGNUM, BIGNUM), (2, BIGNUM, BIGNUM)], result)
예제 #3
0
 def query(s):
     cnn = dbapi.connect(host = DB_HOST, user = DB_USER,
                         password = DB_PASSWD, db = DB_DB)
     cur = cnn.cursor()
     cur.execute("select sleep(%d)" % s)
     cur.close()
     cnn.close()
예제 #4
0
    def testSelectUnicode(self):
        s = u'r\xc3\xa4ksm\xc3\xb6rg\xc3\xa5s'



        cnn = dbapi.connect(host = DB_HOST, user = DB_USER,
                            password = DB_PASSWD, db = DB_DB,
                            charset = 'latin-1', use_unicode = True)

        cur = cnn.cursor()

        cur.execute("truncate tbltest")
        cur.execute("insert into tbltest (test_id, test_string) values (%s, %s)", (1, 'piet'))
        cur.execute("insert into tbltest (test_id, test_string) values (%s, %s)", (2, s))
        cur.execute(u"insert into tbltest (test_id, test_string) values (%s, %s)", (3, s))

        cur.execute("select test_id, test_string from tbltest")

        result = cur.fetchall()

        self.assertEquals([(1, u'piet'), (2, s), (3, s)], result)

        #test that we can still cleanly roundtrip a blob, (it should not be encoded if we pass
        #it as 'str' argument), eventhough we pass the qry itself as unicode
        blob = ''.join([chr(i) for i in range(256)])

        cur.execute(u"insert into tbltest (test_id, test_blob) values (%s, %s)", (4, blob))
        cur.execute("select test_blob from tbltest where test_id = %s", (4,))
        b2 = cur.fetchall()[0][0]
        self.assertEquals(str, type(b2))
        self.assertEquals(256, len(b2))
        self.assertEquals(blob, b2)
예제 #5
0
    def testBlob(self):
        peacesign_binary = "\xe2\x98\xae"
        peacesign_binary2 = "\xe2\x98\xae" * 1024

        cnn = dbapi.connect(host=DB_HOST,
                            user=DB_USER,
                            password=DB_PASSWD,
                            db=DB_DB,
                            charset='latin-1',
                            use_unicode=True)

        cur = cnn.cursor()
        cur.execute("drop table if exists tblblob")
        cur.execute(
            "create table tblblob (test_id int(11) DEFAULT NULL, test_blob BLOB DEFAULT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8"
        )

        cur.execute("insert into tblblob (test_id, test_blob) values (%s, %s)",
                    (1, peacesign_binary))
        cur.execute("insert into tblblob (test_id, test_blob) values (%s, %s)",
                    (2, peacesign_binary2))

        cur.execute("select test_id, test_blob from tblblob")
        result = cur.fetchall()

        # We expect binary strings back
        self.assertEquals([(1, peacesign_binary), (2, peacesign_binary2)],
                          result)
예제 #6
0
    def testDateTime(self):
        """Tests the behaviour of insert/select with mysql/DATETIME <-> python/datetime.datetime"""

        d_date = datetime.datetime(2010, 02, 11, 13, 37, 42)
        d_string = "2010-02-11 13:37:42"

        cnn = dbapi.connect(host = DB_HOST, user = DB_USER,
                            password = DB_PASSWD, db = DB_DB,
                            charset = 'latin-1', use_unicode = True)

        cur = cnn.cursor()

        cur.execute("drop table if exists tbldate")
        cur.execute("create table tbldate (test_id int(11) DEFAULT NULL, test_date datetime DEFAULT NULL, test_date2 datetime DEFAULT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1")

        cur.execute("insert into tbldate (test_id, test_date, test_date2) values (%s, '" + d_string + "', %s)", (1, d_date))

        # Make sure our insert was correct
        cur.execute("select test_id from tbldate where test_date = test_date2")
        result = cur.fetchall()
        self.assertEquals([(1, )], result)

        # Make sure select gets the right value back
        cur.execute("select test_id, test_date, test_date2 from tbldate where test_date = test_date2")
        result = cur.fetchall()
        self.assertEquals([(1, d_date, d_date)], result)
예제 #7
0
    def testCharsets(self):
        aumlaut_unicode = u"\u00e4"
        aumlaut_utf8 = "\xc3\xa4"
        aumlaut_latin1 = "\xe4"


        cnn = dbapi.connect(host = DB_HOST, user = DB_USER,
                            password = DB_PASSWD, db = DB_DB,
                            charset = 'utf8', use_unicode = True)

        cur = cnn.cursor()
        cur.execute("drop table if exists tblutf")
        cur.execute("create table tblutf (test_mode VARCHAR(32) DEFAULT NULL, test_utf VARCHAR(32) DEFAULT NULL, test_latin1 VARCHAR(32)) ENGINE=MyISAM DEFAULT CHARSET=utf8")

        # We insert the same character using two different encodings
        cur.execute("set names utf8")
        cur.execute("insert into tblutf (test_mode, test_utf, test_latin1) values ('utf8', _utf8'" + aumlaut_utf8 + "', _latin1'" + aumlaut_latin1 + "')")
        
        cur.execute("set names latin1")
        cur.execute("insert into tblutf (test_mode, test_utf, test_latin1) values ('latin1', _utf8'" + aumlaut_utf8 + "', _latin1'" + aumlaut_latin1 + "')")

        # We expect the driver to always give us unicode strings back
        expected = [(u"utf8", aumlaut_unicode, aumlaut_unicode), (u"latin1", aumlaut_unicode, aumlaut_unicode)]

        # Fetch and test with different charsets
        for charset in ("latin1", "utf8", "cp1250"):
            cur.execute("set names " + charset)
            cur.execute("select test_mode, test_utf, test_latin1 from tblutf")
            result = cur.fetchall()
            self.assertEquals(result, expected)
예제 #8
0
    def testDateTime(self):
        """Tests the behaviour of insert/select with mysql/DATETIME <-> python/datetime.datetime"""

        d_date = datetime.datetime(2010, 02, 11, 13, 37, 42)
        d_string = "2010-02-11 13:37:42"

        cnn = dbapi.connect(host=DB_HOST,
                            user=DB_USER,
                            password=DB_PASSWD,
                            db=DB_DB,
                            charset='latin-1',
                            use_unicode=True)

        cur = cnn.cursor()

        cur.execute("drop table if exists tbldate")
        cur.execute(
            "create table tbldate (test_id int(11) DEFAULT NULL, test_date datetime DEFAULT NULL, test_date2 datetime DEFAULT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1"
        )

        cur.execute(
            "insert into tbldate (test_id, test_date, test_date2) values (%s, '"
            + d_string + "', %s)", (1, d_date))

        # Make sure our insert was correct
        cur.execute("select test_id from tbldate where test_date = test_date2")
        result = cur.fetchall()
        self.assertEquals([(1, )], result)

        # Make sure select gets the right value back
        cur.execute(
            "select test_id, test_date, test_date2 from tbldate where test_date = test_date2"
        )
        result = cur.fetchall()
        self.assertEquals([(1, d_date, d_date)], result)
예제 #9
0
    def testZeroDates(self):
        """Tests the behaviour of zero dates"""

        zero_datetime = "0000-00-00 00:00:00"
        zero_date = "0000-00-00"

        cnn = dbapi.connect(host=DB_HOST,
                            user=DB_USER,
                            password=DB_PASSWD,
                            db=DB_DB,
                            charset='latin-1',
                            use_unicode=True)

        cur = cnn.cursor()

        cur.execute("drop table if exists tbldate")
        cur.execute(
            "create table tbldate (test_id int(11) DEFAULT NULL, test_date date DEFAULT NULL, test_datetime datetime DEFAULT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1"
        )

        cur.execute(
            "insert into tbldate (test_id, test_date, test_datetime) values (%s, %s, %s)",
            (1, zero_date, zero_datetime))

        # Make sure we get None-values back
        cur.execute(
            "select test_id, test_date, test_datetime from tbldate where test_id = 1"
        )
        result = cur.fetchall()
        self.assertEquals([(1, None, None)], result)
예제 #10
0
 def query(s):
     cnn = dbapi.connect(host=DB_HOST,
                         user=DB_USER,
                         password=DB_PASSWD,
                         db=DB_DB)
     cur = cnn.cursor()
     cur.execute("select sleep(%d)" % s)
     cur.close()
     cnn.close()
예제 #11
0
    def testAutoInc(self):

        cnn = dbapi.connect(host=DB_HOST,
                            user=DB_USER,
                            password=DB_PASSWD,
                            db=DB_DB)

        cur = cnn.cursor()

        cur.execute("truncate tblautoincint")

        cur.execute("ALTER TABLE tblautoincint AUTO_INCREMENT = 100")
        cur.execute("insert into tblautoincint (test_string) values (%s)",
                    ('piet', ))
        self.assertEqual(1, cur.rowcount)
        self.assertEqual(100, cur.lastrowid)
        cur.execute("insert into tblautoincint (test_string) values (%s)",
                    ('piet', ))
        self.assertEqual(1, cur.rowcount)
        self.assertEqual(101, cur.lastrowid)

        cur.execute("ALTER TABLE tblautoincint AUTO_INCREMENT = 4294967294")
        cur.execute("insert into tblautoincint (test_string) values (%s)",
                    ('piet', ))
        self.assertEqual(1, cur.rowcount)
        self.assertEqual(4294967294, cur.lastrowid)
        cur.execute("insert into tblautoincint (test_string) values (%s)",
                    ('piet', ))
        self.assertEqual(1, cur.rowcount)
        self.assertEqual(4294967295, cur.lastrowid)

        cur.execute("truncate tblautoincbigint")

        cur.execute("ALTER TABLE tblautoincbigint AUTO_INCREMENT = 100")
        cur.execute("insert into tblautoincbigint (test_string) values (%s)",
                    ('piet', ))
        self.assertEqual(1, cur.rowcount)
        self.assertEqual(100, cur.lastrowid)
        cur.execute("insert into tblautoincbigint (test_string) values (%s)",
                    ('piet', ))
        self.assertEqual(1, cur.rowcount)
        self.assertEqual(101, cur.lastrowid)

        cur.execute(
            "ALTER TABLE tblautoincbigint AUTO_INCREMENT = 18446744073709551614"
        )
        cur.execute("insert into tblautoincbigint (test_string) values (%s)",
                    ('piet', ))
        self.assertEqual(1, cur.rowcount)
        self.assertEqual(18446744073709551614, cur.lastrowid)
        #this fails on mysql, but that is a mysql problem
        #cur.execute("insert into tblautoincbigint (test_string) values (%s)", ('piet',))
        #self.assertEqual(1, cur.rowcount)
        #self.assertEqual(18446744073709551615, cur.lastrowid)

        cur.close()
        cnn.close()
예제 #12
0
    def testEscapeArgs(self):
        cnn = dbapi.connect(host=DB_HOST,
                            user=DB_USER,
                            password=DB_PASSWD,
                            db=DB_DB)

        cur = cnn.cursor()

        cur.execute("truncate tbltest")

        cur.execute(
            "insert into tbltest (test_id, test_string) values (%s, %s)",
            (1, 'piet'))
        cur.execute(
            "insert into tbltest (test_id, test_string) values (%s, %s)",
            (2, 'klaas'))
        cur.execute(
            "insert into tbltest (test_id, test_string) values (%s, %s)",
            (3, "pi'et"))

        #classic sql injection, would return all rows if no proper escaping is done
        cur.execute(
            "select test_id, test_string from tbltest where test_string = %s",
            ("piet' OR 'a' = 'a", ))
        self.assertEquals([], cur.fetchall())  #assert no rows are found

        #but we should still be able to find the piet with the apostrophe in its name
        cur.execute(
            "select test_id, test_string from tbltest where test_string = %s",
            ("pi'et", ))
        self.assertEquals([(3, "pi'et")], cur.fetchall())

        #also we should be able to insert and retrieve blob/string with all possible bytes transparently
        chars = ''.join([chr(i) for i in range(256)])

        cur.execute(
            "insert into tbltest (test_id, test_string, test_blob) values (%s, %s, %s)",
            (4, chars, chars))

        cur.execute(
            "select test_string, test_blob from tbltest where test_id = %s",
            (4, ))
        #self.assertEquals([(chars, chars)], cur.fetchall())
        s, b = cur.fetchall()[0]

        #test blob
        self.assertEquals(256, len(b))
        self.assertEquals(chars, b)

        #test string
        self.assertEquals(256, len(s))
        self.assertEquals(chars, s)

        cur.close()

        cnn.close()
예제 #13
0
    def testMySQLDBAPI(self):

        cnn = dbapi.connect(host=DB_HOST,
                            user=DB_USER,
                            password=DB_PASSWD,
                            db=DB_DB)

        cur = cnn.cursor()

        cur.execute("truncate tbltest")

        for i in range(10):
            cur.execute(
                "insert into tbltest (test_id, test_string) values (%d, 'test%d')"
                % (i, i))

        cur.close()

        cur = cnn.cursor()

        cur.execute("select test_id, test_string from tbltest")

        self.assertEquals((0, 'test0'), cur.fetchone())

        #check that fetchall gets the remainder
        self.assertEquals([(1, 'test1'), (2, 'test2'), (3, 'test3'),
                           (4, 'test4'), (5, 'test5'), (6, 'test6'),
                           (7, 'test7'), (8, 'test8'), (9, 'test9')],
                          cur.fetchall())

        #another query on the same cursor should work
        cur.execute("select test_id, test_string from tbltest")

        #fetch some but not all
        self.assertEquals((0, 'test0'), cur.fetchone())
        self.assertEquals((1, 'test1'), cur.fetchone())
        self.assertEquals((2, 'test2'), cur.fetchone())

        #close whould work even with half read resultset
        cur.close()

        #this should not work, cursor was closed
        try:
            cur.execute("select * from tbltest")
            self.fail("expected exception")
        except dbapi.ProgrammingError:
            pass
예제 #14
0
    def testAutoInc(self):

        cnn = dbapi.connect(host = DB_HOST, user = DB_USER,
                            password = DB_PASSWD, db = DB_DB)

        cur = cnn.cursor()

        cur.execute("truncate tblautoincint")

        cur.execute("ALTER TABLE tblautoincint AUTO_INCREMENT = 100")
        cur.execute("insert into tblautoincint (test_string) values (%s)", ('piet',))
        self.assertEqual(1, cur.rowcount)
        self.assertEqual(100, cur.lastrowid)
        cur.execute("insert into tblautoincint (test_string) values (%s)", ('piet',))
        self.assertEqual(1, cur.rowcount)
        self.assertEqual(101, cur.lastrowid)

        cur.execute("ALTER TABLE tblautoincint AUTO_INCREMENT = 4294967294")
        cur.execute("insert into tblautoincint (test_string) values (%s)", ('piet',))
        self.assertEqual(1, cur.rowcount)
        self.assertEqual(4294967294, cur.lastrowid)
        cur.execute("insert into tblautoincint (test_string) values (%s)", ('piet',))
        self.assertEqual(1, cur.rowcount)
        self.assertEqual(4294967295, cur.lastrowid)

        cur.execute("truncate tblautoincbigint")

        cur.execute("ALTER TABLE tblautoincbigint AUTO_INCREMENT = 100")
        cur.execute("insert into tblautoincbigint (test_string) values (%s)", ('piet',))
        self.assertEqual(1, cur.rowcount)
        self.assertEqual(100, cur.lastrowid)
        cur.execute("insert into tblautoincbigint (test_string) values (%s)", ('piet',))
        self.assertEqual(1, cur.rowcount)
        self.assertEqual(101, cur.lastrowid)

        cur.execute("ALTER TABLE tblautoincbigint AUTO_INCREMENT = 18446744073709551614")
        cur.execute("insert into tblautoincbigint (test_string) values (%s)", ('piet',))
        self.assertEqual(1, cur.rowcount)
        self.assertEqual(18446744073709551614, cur.lastrowid)
        #this fails on mysql, but that is a mysql problem
        #cur.execute("insert into tblautoincbigint (test_string) values (%s)", ('piet',))
        #self.assertEqual(1, cur.rowcount)
        #self.assertEqual(18446744073709551615, cur.lastrowid)

        cur.close()
        cnn.close()
예제 #15
0
    def testUnicodeUTF8(self):
        peacesign_unicode = u"\u262e"
        peacesign_utf8 = "\xe2\x98\xae"

        cnn = dbapi.connect(host = DB_HOST, user = DB_USER,
                            password = DB_PASSWD, db = DB_DB,
                            charset = 'utf-8', use_unicode = True)

        cur = cnn.cursor()
        cur.execute("drop table if exists tblutf")
        cur.execute("create table tblutf (test_id int(11) DEFAULT NULL, test_string VARCHAR(32) DEFAULT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8")

        cur.execute("insert into tblutf (test_id, test_string) values (%s, %s)", (1, peacesign_unicode)) # This should be encoded in utf8
        cur.execute("insert into tblutf (test_id, test_string) values (%s, %s)", (2, peacesign_utf8))

        cur.execute("select test_id, test_string from tblutf")
        result = cur.fetchall()

        # We expect unicode strings back
        self.assertEquals([(1, peacesign_unicode), (2, peacesign_unicode)], result)
예제 #16
0
    def testBlob(self):
        peacesign_binary = "\xe2\x98\xae"
        peacesign_binary2 = "\xe2\x98\xae" * 1024

        cnn = dbapi.connect(host = DB_HOST, user = DB_USER,
                            password = DB_PASSWD, db = DB_DB,
                            charset = 'latin-1', use_unicode = True)

        cur = cnn.cursor()
        cur.execute("drop table if exists tblblob")
        cur.execute("create table tblblob (test_id int(11) DEFAULT NULL, test_blob BLOB DEFAULT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8")

        cur.execute("insert into tblblob (test_id, test_blob) values (%s, %s)", (1, peacesign_binary))
        cur.execute("insert into tblblob (test_id, test_blob) values (%s, %s)", (2, peacesign_binary2))

        cur.execute("select test_id, test_blob from tblblob")
        result = cur.fetchall()

        # We expect binary strings back
        self.assertEquals([(1, peacesign_binary),(2, peacesign_binary2)], result)
예제 #17
0
    def testEscapeArgs(self):
        cnn = dbapi.connect(host = DB_HOST, user = DB_USER,
                            password = DB_PASSWD, db = DB_DB)

        cur = cnn.cursor()

        cur.execute("truncate tbltest")

        cur.execute("insert into tbltest (test_id, test_string) values (%s, %s)", (1, 'piet'))
        cur.execute("insert into tbltest (test_id, test_string) values (%s, %s)", (2, 'klaas'))
        cur.execute("insert into tbltest (test_id, test_string) values (%s, %s)", (3, "pi'et"))

        #classic sql injection, would return all rows if no proper escaping is done
        cur.execute("select test_id, test_string from tbltest where test_string = %s", ("piet' OR 'a' = 'a",))
        self.assertEquals([], cur.fetchall()) #assert no rows are found

        #but we should still be able to find the piet with the apostrophe in its name
        cur.execute("select test_id, test_string from tbltest where test_string = %s", ("pi'et",))
        self.assertEquals([(3, "pi'et")], cur.fetchall())

        #also we should be able to insert and retrieve blob/string with all possible bytes transparently
        chars = ''.join([chr(i) for i in range(256)])


        cur.execute("insert into tbltest (test_id, test_string, test_blob) values (%s, %s, %s)", (4, chars, chars))

        cur.execute("select test_string, test_blob from tbltest where test_id = %s", (4,))
        #self.assertEquals([(chars, chars)], cur.fetchall())
        s, b  = cur.fetchall()[0]

        #test blob
        self.assertEquals(256, len(b))
        self.assertEquals(chars, b)

        #test string
        self.assertEquals(256, len(s))
        self.assertEquals(chars, s)

        cur.close()

        cnn.close()
예제 #18
0
    def testSelectUnicode(self):
        s = u'r\xc3\xa4ksm\xc3\xb6rg\xc3\xa5s'

        cnn = dbapi.connect(host=DB_HOST,
                            user=DB_USER,
                            password=DB_PASSWD,
                            db=DB_DB,
                            charset='latin-1',
                            use_unicode=True)

        cur = cnn.cursor()

        cur.execute("truncate tbltest")
        cur.execute(
            "insert into tbltest (test_id, test_string) values (%s, %s)",
            (1, 'piet'))
        cur.execute(
            "insert into tbltest (test_id, test_string) values (%s, %s)",
            (2, s))
        cur.execute(
            u"insert into tbltest (test_id, test_string) values (%s, %s)",
            (3, s))

        cur.execute("select test_id, test_string from tbltest")

        result = cur.fetchall()

        self.assertEquals([(1, u'piet'), (2, s), (3, s)], result)

        #test that we can still cleanly roundtrip a blob, (it should not be encoded if we pass
        #it as 'str' argument), eventhough we pass the qry itself as unicode
        blob = ''.join([chr(i) for i in range(256)])

        cur.execute(
            u"insert into tbltest (test_id, test_blob) values (%s, %s)",
            (4, blob))
        cur.execute("select test_blob from tbltest where test_id = %s", (4, ))
        b2 = cur.fetchall()[0][0]
        self.assertEquals(str, type(b2))
        self.assertEquals(256, len(b2))
        self.assertEquals(blob, b2)
예제 #19
0
    def testMySQLDBAPI(self):

        cnn = dbapi.connect(host = DB_HOST, user = DB_USER,
                            password = DB_PASSWD, db = DB_DB)

        cur = cnn.cursor()

        cur.execute("truncate tbltest")

        for i in range(10):
            cur.execute("insert into tbltest (test_id, test_string) values (%d, 'test%d')" % (i, i))

        cur.close()

        cur = cnn.cursor()

        cur.execute("select test_id, test_string from tbltest")

        self.assertEquals((0, 'test0'), cur.fetchone())

        #check that fetchall gets the remainder
        self.assertEquals([(1, 'test1'), (2, 'test2'), (3, 'test3'), (4, 'test4'), (5, 'test5'), (6, 'test6'), (7, 'test7'), (8, 'test8'), (9, 'test9')], cur.fetchall())

        #another query on the same cursor should work
        cur.execute("select test_id, test_string from tbltest")

        #fetch some but not all
        self.assertEquals((0, 'test0'), cur.fetchone())
        self.assertEquals((1, 'test1'), cur.fetchone())
        self.assertEquals((2, 'test2'), cur.fetchone())

        #close whould work even with half read resultset
        cur.close()

        #this should not work, cursor was closed
        try:
            cur.execute("select * from tbltest")
            self.fail("expected exception")
        except dbapi.ProgrammingError:
            pass
예제 #20
0
    def testBigInt(self):
        """Tests the behaviour of insert/select with bigint/long."""

        BIGNUM = 112233445566778899

        cnn = dbapi.connect(host=DB_HOST,
                            user=DB_USER,
                            password=DB_PASSWD,
                            db=DB_DB,
                            charset='latin-1',
                            use_unicode=True)

        cur = cnn.cursor()

        cur.execute("drop table if exists tblbigint")
        cur.execute("""create table tblbigint (
                            test_id int(11) DEFAULT NULL,
                            test_bigint bigint DEFAULT NULL,
                            test_bigint2 bigint DEFAULT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1"""
                    )
        cur.execute(
            "insert into tblbigint (test_id, test_bigint, test_bigint2) values (%s, "
            + str(BIGNUM) + ", %s)", (1, BIGNUM))
        cur.execute(
            u"insert into tblbigint (test_id, test_bigint, test_bigint2) values (%s, "
            + str(BIGNUM) + ", %s)", (2, BIGNUM))

        # Make sure both our inserts where correct (ie, the big number was not truncated/modified on insert)
        cur.execute(
            "select test_id from tblbigint where test_bigint = test_bigint2")
        result = cur.fetchall()
        self.assertEquals([(1, ), (2, )], result)

        # Make sure select gets the right values (ie, the big number was not truncated/modified when retrieved)
        cur.execute(
            "select test_id, test_bigint, test_bigint2 from tblbigint where test_bigint = test_bigint2"
        )
        result = cur.fetchall()
        self.assertEquals([(1, BIGNUM, BIGNUM), (2, BIGNUM, BIGNUM)], result)
예제 #21
0
    def testCharsets(self):
        aumlaut_unicode = u"\u00e4"
        aumlaut_utf8 = "\xc3\xa4"
        aumlaut_latin1 = "\xe4"

        cnn = dbapi.connect(host=DB_HOST,
                            user=DB_USER,
                            password=DB_PASSWD,
                            db=DB_DB,
                            charset='utf8',
                            use_unicode=True)

        cur = cnn.cursor()
        cur.execute("drop table if exists tblutf")
        cur.execute(
            "create table tblutf (test_mode VARCHAR(32) DEFAULT NULL, test_utf VARCHAR(32) DEFAULT NULL, test_latin1 VARCHAR(32)) ENGINE=MyISAM DEFAULT CHARSET=utf8"
        )

        # We insert the same character using two different encodings
        cur.execute("set names utf8")
        cur.execute(
            "insert into tblutf (test_mode, test_utf, test_latin1) values ('utf8', _utf8'"
            + aumlaut_utf8 + "', _latin1'" + aumlaut_latin1 + "')")

        cur.execute("set names latin1")
        cur.execute(
            "insert into tblutf (test_mode, test_utf, test_latin1) values ('latin1', _utf8'"
            + aumlaut_utf8 + "', _latin1'" + aumlaut_latin1 + "')")

        # We expect the driver to always give us unicode strings back
        expected = [(u"utf8", aumlaut_unicode, aumlaut_unicode),
                    (u"latin1", aumlaut_unicode, aumlaut_unicode)]

        # Fetch and test with different charsets
        for charset in ("latin1", "utf8", "cp1250"):
            cur.execute("set names " + charset)
            cur.execute("select test_mode, test_utf, test_latin1 from tblutf")
            result = cur.fetchall()
            self.assertEquals(result, expected)
예제 #22
0
    def testZeroDates(self):
        """Tests the behaviour of zero dates"""

        zero_datetime = "0000-00-00 00:00:00" 
        zero_date = "0000-00-00"


        cnn = dbapi.connect(host = DB_HOST, user = DB_USER,
                            password = DB_PASSWD, db = DB_DB,
                            charset = 'latin-1', use_unicode = True)

        cur = cnn.cursor()

        cur.execute("drop table if exists tbldate")
        cur.execute("create table tbldate (test_id int(11) DEFAULT NULL, test_date date DEFAULT NULL, test_datetime datetime DEFAULT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1")

        cur.execute("insert into tbldate (test_id, test_date, test_datetime) values (%s, %s, %s)", (1, zero_date, zero_datetime))

        # Make sure we get None-values back
        cur.execute("select test_id, test_date, test_datetime from tbldate where test_id = 1")
        result = cur.fetchall()
        self.assertEquals([(1, None, None)], result)
예제 #23
0
def task():
    conn = geventmysql.connect(host="127.0.0.1", user="******", password="")
    cur = conn.cursor()
    for i in range(N):
        cur.execute("SELECT 1")
        res = cur.fetchall()
예제 #24
0
def task():
    conn = geventmysql.connect(host="127.0.0.1", user="******", password="")
    cur = conn.cursor()
    for i in range(N):
        cur.execute("SELECT 1")
        res = cur.fetchall()
예제 #25
0
import geventmysql

conn = geventmysql.connect(host="127.0.0.1", user="******", password="******")

cursor = conn.cursor()
cursor.execute("create database if not exists test_nested")
cursor.execute("use test_nested")
cursor.execute("create table if not exists numbers(a int)")
cursor.execute("delete from numbers")
cursor.execute("insert into numbers(a) values (1),(2),(3),(4),(5)")
cursor.execute("SELECT * from numbers")

for a, in cursor.fetchall():
	cursor.execute("SELECT * from numbers")
	for b, in cursor.fetchall():
		print a,b


cursor.close()
conn.close()

        
예제 #26
0
파일: mysql.py 프로젝트: irr/python-labs
import geventmysql

conn = geventmysql.connect(host="127.0.0.1", user="******", password="******")

cursor = conn.cursor()
cursor.execute("SELECT 1")

print cursor.fetchall()

cursor.close()
conn.close()