Skip to content

Instantly share code, notes, and snippets.

@Gelbpunkt
Last active April 30, 2018 17:39
Show Gist options
  • Save Gelbpunkt/4c9bc0275e50b1c1d398d775490f3cfa to your computer and use it in GitHub Desktop.
Save Gelbpunkt/4c9bc0275e50b1c1d398d775490f3cfa to your computer and use it in GitHub Desktop.
Discord Bot aiopg example
import aiopg
#attach the pool to the bot
async def create_pool():
connstring = 'dbname=databasename user=databaseuser password=databasepassword host=databasehost'
pool = await aiopg.create_pool(connstring)
return pool
async def start_bot():
pool = await create_pool()
client.pool = pool
await client.start("token")
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(start_bot())
#in any command
@client.command()
async def test():
async with client.pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute("SELECT 1;")
result = await cur.fetchall()
#injection safe when using vars
myvar = "hello"
await cur.execute('SELECT * FROM table WHERE "column_value"=%s;', (myvar,))
#use a tuple to specify the values to use for %s
#%s is used also for integers, booleans and others
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment