" -----------------------------------------------------------------------------"" UW Query Database" ~~~~~~~~~~~~~~~~~~~~"" Execute any query on the database and get the result onto the current" buffer."" 0. Source this file :source %." 1. Open a new buffer." 2. Enter your query in the first line." 3. Press ';s'." 4. Buffer will be filled with the results." 5. Press 'u' and edit again." 6. Press ';s'." 7. Continue until you get what you want." 8. Process with the Vim, Python or Unix tools."" Maintainer: Upeksha Wisidagama" License: GNU GPL v3 or later"" -----------------------------------------------------------------------------" Sample Queries"" You can keep your frequently used queries in this area."" -----------------------------------------------------------------------------:fun!UW_Query_Database()python<<endpythonimportvimimportMySQLdbasmdbdefuw_query_database():current_buffer=vim.current.bufferuw_query=current_buffer[0]data=uw_query_db(uw_query)uw_write_buffer(data)defuw_query_db(uw_query):try:con=mdb.connect('HOSTNAME','USER','PASSWORD','DB_NAME');cur=con.cursor()cur.execute(uw_query)data=cur.fetchall()returndataexceptmdb.Error,e:print"Error %d: %s"%(e.args[0],e.args[1])sys.exit(1)finally:ifcon:con.close()defuw_write_buffer(data):current_buffer=vim.current.bufferi=0current_buff=[]fordatumindata:record=''forfieldindatum:record=record+str(field)+'\t'current_buff.append(record)i=i+1current_buffer[:]=current_buffvim.command('%!column -t')uw_query_database()endpython:endfun" -----------------------------------------------------------------------------:noremap<silent>;s:callUW_Query_Database()<CR>" -----------------------------------------------------------------------------
Map to Anything
Currently executing the query is mapped to ;s. But you are free to change it to anything you want.
Enter your query in the first line in a new buffer. Press ‘;s’. Buffer will be filled with the results. Press ‘u’ and edit again. Press ‘;s’. Continue until you get what you want.
You can do all your processing just after retrieving the data from the database, in the buffer itself!
Process data at home in Vim. You can even outsource some tasks to some GNU Tools external to Vim such as sort, column.
Open a new buffer :vnew and enter the following query.
select id, lockcode, model, paid, mask_no from gtk_sections where model = 111 and paid = 'paid' order by mask_no desc
Press ;s. The buffer will be filled with the results.
Suppose you have the following data in your buffer