2017-10-26 12 views
0

Pythonでクエリを実行するときに\ Gオプションを使いたいです。Python、mariaDB: Gオプションの使い方は?

\ Gオプションは、クエリ結果を垂直方向に出力します。

select * from exam\G 
*************************** 1. row *************************** 
col_1: test2 
col_2: test2 
col_3: test2 
*************************** 2. row *************************** 
col_1: test3 
col_2: test3 
col_3: test3 
*************************** 3. row *************************** 
col_1: test4 
col_2: test4 
col_3: test4 

例えば

は)だから私は、クエリを送信するために() "受験の\ G SELECT * FROM" を実行に使用しました。

ただし、このコマンドは実行されませんでした。エラーになりました

db = pymysql.connect(
    host=localhost, port=3306,user=root,passwd=test,db=test,charset=utf8) 

cursor = db.cursor() 
cursor.execute("select * from exam\G") 
data = cursor.fetchall() 

for i in data: 
    print i 

...

pymysql.err.ProgrammingError: (1064, u"You have an error in your SQL syntax; 
check the manual that corresponds to your MariaDB server version for the 
right syntax to use near '\\G' at line 1") 

pymysqlで\ Gオプションを使用することができませんか?

\ Gと同じ結果を得る方法はありますか?

ありがとうございました。

+0

'\ G'はmysqlコマンドラインツールの特徴であり、サーバではありません。 –

答えて

0

\G option is only for displaying results verticallyプロンプトモードです。実際に返された内容は変更されません。

クエリは、セミコロンの代わりに\ Gを使用してクエリを終了することによって垂直に表示できます。

Pythonで同様の効果を繰り返すには、クエリから '\ g'を削除し、結果をループしているときに、その列に対して別のループを実行します。名前+の列の順序を行いますMySQLCursorNamedTuple cursorで行うことが容易になるなど

data = cursor.fetchall() 

for row in data: 
    for colval in row: 
     print colval 

と行と列番号を取得するには、

data = cursor.fetchall() 

for rid, row in enumerate(data, start=1): 
    print '*** row', rowid, '***' 
    for colid, colval in enumerate(row, start=1): 
     print colid, ':', colval 

+0

答えが遅いです。ありがとうございました! – mark3

関連する問題