@@ -3105,8 +3105,14 @@ below and the SQLAlchemy `documentation <http://docs.sqlalchemy.org/en/rel_0_9/c
3105
3105
# Create your connection.
3106
3106
engine = create_engine(' sqlite:///:memory:' )
3107
3107
3108
+ Writing DataFrames
3109
+ ~~~~~~~~~~~~~~~~~~
3108
3110
3111
+ <<<<<<< HEAD
3109
3112
Assuming the following data is in a DataFrame "data", we can insert it into
3113
+ =======
3114
+ Assuming the following data is in a DataFrame ``data ``, we can insert it into
3115
+ >>>>>>> 6314e6f... ENH #4163 Tweaks to docs, avoid mutable default args, mysql tests
3110
3116
the database using :func: `~pandas.io.sql.to_sql `.
3111
3117
3112
3118
@@ -3138,17 +3144,16 @@ the database using :func:`~pandas.io.sql.to_sql`.
3138
3144
3139
3145
data = DataFrame(d, columns = c)
3140
3146
3141
- .. ipython :: python
3142
-
3143
- sql.to_sql(data, ' data' , engine)
3147
+ Reading Tables
3148
+ ~~~~~~~~~~~~~~
3144
3149
3145
- You can read from the database simply by specifying a table
3146
- name using the :func: ` ~pandas.io.sql.read_table ` function .
3150
+ :func: ` ~pandas.io.sql.read_table ` will read a databse table given the
3151
+ table name and optionally a subset of columns to read .
3147
3152
3148
3153
.. note ::
3149
3154
3150
- In order to use read_table, you MUST have the SQLAlchemy optional
3151
- dependency installed.
3155
+ In order to use :func: ` ~pandas.io.sql. read_table` , you ** must ** have the
3156
+ SQLAlchemy optional dependency installed.
3152
3157
3153
3158
.. ipython :: python
3154
3159
@@ -3176,11 +3181,17 @@ to pass to :func:`pandas.tseries.tools.to_datetime`.
3176
3181
sql.read_table(' data' , engine, parse_dates = {' Date' : ' %Y-%m-%d ' })
3177
3182
sql.read_table(' data' , engine, parse_dates = {' Date' : {' format' : ' %Y-%m-%d %H:%M:%S' }})
3178
3183
3184
+
3185
+ You can check if a table exists using :func: `~pandas.io.sql.has_table `
3186
+
3187
+ In addition, the class :class: `~pandas.io.sql.PandasSQLWithEngine ` can be
3188
+ instantiated directly for more manual control over the SQL interaction.
3189
+
3179
3190
Querying
3180
3191
~~~~~~~~
3181
3192
3182
- You can also query using raw SQL in the :func: `~pandas.io.sql.read_sql ` function.
3183
- In this case you must use valid SQL for your database.
3193
+ You can query using raw SQL in the :func: `~pandas.io.sql.read_sql ` function.
3194
+ In this case you must use the SQL variant appropriate for your database.
3184
3195
When using SQLAlchemy, you can also pass SQLAlchemy Expression language constructs,
3185
3196
which are database-agnostic.
3186
3197
@@ -3195,10 +3206,17 @@ Of course, you can specify a more "complex" query.
3195
3206
sql.read_frame(" SELECT id, Col_1, Col_2 FROM data WHERE id = 42;" , engine)
3196
3207
3197
3208
3198
- There are a few other available functions:
3209
+ You can also run a plain query without creating a dataframe with
3210
+ :func: `~pandas.io.sql.execute `. This is useful for queries that don't return values,
3211
+ such as INSERT. This is functionally equivalent to calling ``execute `` on the
3212
+ SQLAlchemy engine or db connection object. Again, ou must use the SQL syntax
3213
+ variant appropriate for your database.
3214
+
3215
+ .. code-block :: python
3199
3216
3200
- :func: ` ~pandas.io.sql.has_table ` checks if a given table exists.
3217
+ sql.execute( ' SELECT * FROM table_name ' , engine)
3201
3218
3219
+ <<<<<<< HEAD
3202
3220
<<<<<<< HEAD
3203
3221
:func: `~pandas.io.sql.tquery ` returns a list of tuples corresponding to each row.
3204
3222
@@ -3209,6 +3227,10 @@ returning results it returns the number of related rows.
3209
3227
3210
3228
In addition, the class :class: `~pandas.io.sql.PandasSQLWithEngine ` can be
3211
3229
instantiated directly for more manual control over the SQL interaction.
3230
+ =======
3231
+ sql.execute('INSERT INTO table_name VALUES(?, ?, ?)', engine, params=[('id', 1, 12.2, True)])
3232
+
3233
+ >>>>>>> 6314e6f... ENH #4163 Tweaks to docs, avoid mutable default args, mysql tests
3212
3234
3213
3235
Engine connection examples
3214
3236
~~~~~~~~~~~~~~~~~~~~~~~~~~
0 commit comments