Python pandas.io.sql.table_exists() Examples
The following are 7
code examples of pandas.io.sql.table_exists().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pandas.io.sql
, or try the search function
.
Example #1
Source File: test_sql.py From recruit with Apache License 2.0 | 6 votes |
def test_illegal_names(self): # For sqlite, these should work fine df = DataFrame([[1, 2], [3, 4]], columns=['a', 'b']) # Raise error on blank pytest.raises(ValueError, df.to_sql, "", self.conn) for ndx, weird_name in enumerate( ['test_weird_name]', 'test_weird_name[', 'test_weird_name`', 'test_weird_name"', 'test_weird_name\'', '_b.test_weird_name_01-30', '"_b.test_weird_name_01-30"', '99beginswithnumber', '12345', u'\xe9']): df.to_sql(weird_name, self.conn) sql.table_exists(weird_name, self.conn) df2 = DataFrame([[1, 2], [3, 4]], columns=['a', weird_name]) c_tbl = 'test_weird_col_name%d' % ndx df2.to_sql(c_tbl, self.conn) sql.table_exists(c_tbl, self.conn) # ----------------------------------------------------------------------------- # -- Old tests from 0.13.1 (before refactor using sqlalchemy)
Example #2
Source File: test_sql.py From vnpy_crypto with MIT License | 6 votes |
def test_illegal_names(self): # For sqlite, these should work fine df = DataFrame([[1, 2], [3, 4]], columns=['a', 'b']) # Raise error on blank pytest.raises(ValueError, df.to_sql, "", self.conn) for ndx, weird_name in enumerate( ['test_weird_name]', 'test_weird_name[', 'test_weird_name`', 'test_weird_name"', 'test_weird_name\'', '_b.test_weird_name_01-30', '"_b.test_weird_name_01-30"', '99beginswithnumber', '12345', u'\xe9']): df.to_sql(weird_name, self.conn) sql.table_exists(weird_name, self.conn) df2 = DataFrame([[1, 2], [3, 4]], columns=['a', weird_name]) c_tbl = 'test_weird_col_name%d' % ndx df2.to_sql(c_tbl, self.conn) sql.table_exists(c_tbl, self.conn) # ----------------------------------------------------------------------------- # -- Old tests from 0.13.1 (before refactor using sqlalchemy)
Example #3
Source File: test_sql.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
def test_illegal_names(self): # For sqlite, these should work fine df = DataFrame([[1, 2], [3, 4]], columns=['a', 'b']) # Raise error on blank pytest.raises(ValueError, df.to_sql, "", self.conn) for ndx, weird_name in enumerate( ['test_weird_name]', 'test_weird_name[', 'test_weird_name`', 'test_weird_name"', 'test_weird_name\'', '_b.test_weird_name_01-30', '"_b.test_weird_name_01-30"', '99beginswithnumber', '12345', u'\xe9']): df.to_sql(weird_name, self.conn) sql.table_exists(weird_name, self.conn) df2 = DataFrame([[1, 2], [3, 4]], columns=['a', weird_name]) c_tbl = 'test_weird_col_name%d' % ndx df2.to_sql(c_tbl, self.conn) sql.table_exists(c_tbl, self.conn) # ----------------------------------------------------------------------------- # -- Old tests from 0.13.1 (before refactor using sqlalchemy)
Example #4
Source File: test_sql.py From elasticintel with GNU General Public License v3.0 | 6 votes |
def test_illegal_names(self): # For sqlite, these should work fine df = DataFrame([[1, 2], [3, 4]], columns=['a', 'b']) # Raise error on blank pytest.raises(ValueError, df.to_sql, "", self.conn) for ndx, weird_name in enumerate( ['test_weird_name]', 'test_weird_name[', 'test_weird_name`', 'test_weird_name"', 'test_weird_name\'', '_b.test_weird_name_01-30', '"_b.test_weird_name_01-30"', '99beginswithnumber', '12345', u'\xe9']): df.to_sql(weird_name, self.conn) sql.table_exists(weird_name, self.conn) df2 = DataFrame([[1, 2], [3, 4]], columns=['a', weird_name]) c_tbl = 'test_weird_col_name%d' % ndx df2.to_sql(c_tbl, self.conn) sql.table_exists(c_tbl, self.conn) # ----------------------------------------------------------------------------- # -- Old tests from 0.13.1 (before refactor using sqlalchemy)
Example #5
Source File: test_sql.py From twitter-stock-recommendation with MIT License | 6 votes |
def test_illegal_names(self): # For sqlite, these should work fine df = DataFrame([[1, 2], [3, 4]], columns=['a', 'b']) # Raise error on blank pytest.raises(ValueError, df.to_sql, "", self.conn) for ndx, weird_name in enumerate( ['test_weird_name]', 'test_weird_name[', 'test_weird_name`', 'test_weird_name"', 'test_weird_name\'', '_b.test_weird_name_01-30', '"_b.test_weird_name_01-30"', '99beginswithnumber', '12345', u'\xe9']): df.to_sql(weird_name, self.conn) sql.table_exists(weird_name, self.conn) df2 = DataFrame([[1, 2], [3, 4]], columns=['a', weird_name]) c_tbl = 'test_weird_col_name%d' % ndx df2.to_sql(c_tbl, self.conn) sql.table_exists(c_tbl, self.conn) # ----------------------------------------------------------------------------- # -- Old tests from 0.13.1 (before refactor using sqlalchemy)
Example #6
Source File: test_sql.py From Computable with MIT License | 4 votes |
def test_if_exists(self): df_if_exists_1 = DataFrame({'col1': [1, 2], 'col2': ['A', 'B']}) df_if_exists_2 = DataFrame({'col1': [3, 4, 5], 'col2': ['C', 'D', 'E']}) table_name = 'table_if_exists' sql_select = "SELECT * FROM %s" % table_name def clean_up(test_table_to_drop): """ Drops tables created from individual tests so no dependencies arise from sequential tests """ if sql.table_exists(test_table_to_drop, self.db, flavor='sqlite'): cur = self.db.cursor() cur.execute("DROP TABLE %s" % test_table_to_drop) cur.close() # test if invalid value for if_exists raises appropriate error self.assertRaises(ValueError, sql.write_frame, frame=df_if_exists_1, con=self.db, name=table_name, flavor='sqlite', if_exists='notvalidvalue') clean_up(table_name) # test if_exists='fail' sql.write_frame(frame=df_if_exists_1, con=self.db, name=table_name, flavor='sqlite', if_exists='fail') self.assertRaises(ValueError, sql.write_frame, frame=df_if_exists_1, con=self.db, name=table_name, flavor='sqlite', if_exists='fail') # test if_exists='replace' sql.write_frame(frame=df_if_exists_1, con=self.db, name=table_name, flavor='sqlite', if_exists='replace') self.assertEqual(sql.tquery(sql_select, con=self.db), [(1, 'A'), (2, 'B')]) sql.write_frame(frame=df_if_exists_2, con=self.db, name=table_name, flavor='sqlite', if_exists='replace') self.assertEqual(sql.tquery(sql_select, con=self.db), [(3, 'C'), (4, 'D'), (5, 'E')]) clean_up(table_name) # test if_exists='append' sql.write_frame(frame=df_if_exists_1, con=self.db, name=table_name, flavor='sqlite', if_exists='fail') self.assertEqual(sql.tquery(sql_select, con=self.db), [(1, 'A'), (2, 'B')]) sql.write_frame(frame=df_if_exists_2, con=self.db, name=table_name, flavor='sqlite', if_exists='append') self.assertEqual(sql.tquery(sql_select, con=self.db), [(1, 'A'), (2, 'B'), (3, 'C'), (4, 'D'), (5, 'E')]) clean_up(table_name)
Example #7
Source File: test_sql.py From Computable with MIT License | 4 votes |
def test_if_exists(self): _skip_if_no_MySQLdb() df_if_exists_1 = DataFrame({'col1': [1, 2], 'col2': ['A', 'B']}) df_if_exists_2 = DataFrame({'col1': [3, 4, 5], 'col2': ['C', 'D', 'E']}) table_name = 'table_if_exists' sql_select = "SELECT * FROM %s" % table_name def clean_up(test_table_to_drop): """ Drops tables created from individual tests so no dependencies arise from sequential tests """ if sql.table_exists(test_table_to_drop, self.db, flavor='mysql'): cur = self.db.cursor() cur.execute("DROP TABLE %s" % test_table_to_drop) cur.close() # test if invalid value for if_exists raises appropriate error self.assertRaises(ValueError, sql.write_frame, frame=df_if_exists_1, con=self.db, name=table_name, flavor='mysql', if_exists='notvalidvalue') clean_up(table_name) # test if_exists='fail' sql.write_frame(frame=df_if_exists_1, con=self.db, name=table_name, flavor='mysql', if_exists='fail') self.assertRaises(ValueError, sql.write_frame, frame=df_if_exists_1, con=self.db, name=table_name, flavor='mysql', if_exists='fail') # test if_exists='replace' sql.write_frame(frame=df_if_exists_1, con=self.db, name=table_name, flavor='mysql', if_exists='replace') self.assertEqual(sql.tquery(sql_select, con=self.db), [(1, 'A'), (2, 'B')]) sql.write_frame(frame=df_if_exists_2, con=self.db, name=table_name, flavor='mysql', if_exists='replace') self.assertEqual(sql.tquery(sql_select, con=self.db), [(3, 'C'), (4, 'D'), (5, 'E')]) clean_up(table_name) # test if_exists='append' sql.write_frame(frame=df_if_exists_1, con=self.db, name=table_name, flavor='mysql', if_exists='fail') self.assertEqual(sql.tquery(sql_select, con=self.db), [(1, 'A'), (2, 'B')]) sql.write_frame(frame=df_if_exists_2, con=self.db, name=table_name, flavor='mysql', if_exists='append') self.assertEqual(sql.tquery(sql_select, con=self.db), [(1, 'A'), (2, 'B'), (3, 'C'), (4, 'D'), (5, 'E')]) clean_up(table_name)