Ajax + Python + PostgreSQL sample

Introduction

sample

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
</script>

//var data = 10;
//var data_list=['hoge','geho];

function get_data(data, data_list){
  var request = {
    data1 : data,
    data2 : data_list
  };
  $.ajax({
    url: 'get_data.py',
    type: 'POST',
    data: JSON.stringify(request)
  })
  .done(function(data){
    result = JSON.parse(data);
    for (i=0;i<result.length;i++){
      console.log(result[i][0]);
    }
  })
  .fail(function(){
    console.log('failed');
  });
};
import sys
import json
import psycopg2

# connect postgreSQL
users = 'postgres'  
dbnames = 'postgres'
passwords = '*******'

conn = psycopg2.connect(" user=" + users +" dbname=" + dbnames +" password=" + passwords)
cur = conn.cursor() 

# index.Get the data skipped from html
recieve = sys.stdin.read()
params = json.loads(recieve)

data = params["data1"]
data_list = params["data2"]

#Create and execute SQL statement
sql = 'select data from dataTbale where (AAA = data) and ((BBB = data_list[0]) or (BBB = data_list[1]))'
cur.execute(sql)
results = cur.fetchall()
conn.commit()

#Clean up
cur.close()
conn.close()

# index.Notify html side
print('Content-type: text/html\n')
print(json.dumps(results))

Recommended Posts

Ajax + Python + PostgreSQL sample
Python closure sample
Python --Simple multi-thread sample
GitHub Actions Python cache sample
Python higher-order function (decorator) sample
Sample usage of Python pickle
[Python] Sample code for Python grammar
Python
AtCoder: Python: Daddy the sample test.
[Python] Test sample using unittest2, mock
My favorite boto3 (Python) API sample
Install nginx python postgresql using apt-get
Python parallel / parallel processing sample code summary
NAOqi Python sample (make NAO walk)
Use PostgreSQL with Lambda (Python + psycopg2)
Google Cloud Vision API sample for python
[Note] Get data from PostgreSQL with Python
PHP / Python / Ruby sample hitting Path API
Sample script to trap signals in Python
Sample for handling eml files in Python
Ported Python parallel computing sample to F #
Which is faster, Python shuffle or sample?
Load AWS-RDS / PostgreSQL tables with AWS-Lambda + Python
Sample to convert image to Wavelet with Python