While I am trying to run the following basic python script for fetching data in postgresql database using psycopg2 module :
cur.execute("""SELECT project from project_tlb""")
rows=cur.fetchall()
print rows[5]
the output for above I am getting is
('html',)
The value inside the table is html which is of type 'text'. Any suggestions how to get only the value html instead of ('html',) ?
The returned value ('html',) is a tuple so you can just access its first element:
rows[5][0]
to get
'html'
Related
I am working on Django, so I created my table on my models.py. By default when I start registering data, the id, set as primary key, begins with the initial value 1 and increments each time I do a new register.
I have loaded some data in my table, so the final id right now is 185. I run the server and try to add a new register, but I get an error about id duplicity. Django is trying to save the register in id 1.
How do I get it to continue saving the register from id 186 and onwards?
I tried this on my models:
class MyModel(models.Model):
id = models.AutoField(primary_key=True, initial=123)
but it does not recognize the kwarg initial.
You will likely want to use sqlsequencereset.
You can run it in the terminal and it will output SQL commands for resetting the sequence on every table in the specified module.
python manage.py sqlsequencereset <your_app_label>
You can manually run any generated SQL commands, or you can pipe them into django's dbshell to run all of them automatically.
python manage.py sqlsequencereset <your_app_label> | python manage.py dbshell
See this question for more discussion on this topic.
I have a table with a column of type TIMESTAMP WITH TIME ZONE and I try to simply SELECT * FROM table, but I get this message:
postgresql error: '>=' not supported between instances of 'datetime.timedelta' and 'int'`
Am I entering the data incorrectly?
creation_datetime = datetime.now(timezone.utc)
...
new_record = {
"key": valid_value.contract.symbol.lower(),
"datetime_downloaded_from_api": creation_datetime
}
yield new_record
I use meltano and a singer custom TAP to retrieve and input the data.. meltano taps streams.py has the part where I describe the data columns and did this
schema = th.PropertiesList(
th.Property("datetime_downloaded_from_api", th.DateTimeType),
th.Property("contract_id", th.IntegerType)
).to_dict()
turned out I needed a specific version of a python library to be installed
pip install psycopg2-binary==2.8.5
I have a api url.
http://10.236.220.130:5056/api/v1/viewentities?where={"EntityId":"srv-1499839282322"}
my search query variable/key is where={"EntityId":"srv-1499839282322"}
this is my code in python 2.7
tgt="srv-1499839282322"
baseurl="http://10.236.220.130:5056/api/v1/viewentities?"`enter code here`
values={"where": '{"EntityId": "srv-1499839282322"}'}
(if i hardcode this srv stuff above it works)
encode=urllib.urlencode(values)
url=baseurl + encode
print encode
Note the url which am getting is working but my condition is, i want that srv stuff to be a variable that will come as one of my function parameter.
================
I tried this these
tgt="srv-1499839282322"
baseurl="http://10.236.220.130:5056/api/v1/viewentities?"
value={"where": {}}
value["where"]["EntityId"]=tgt
encode=urllib.urlencode(values)
url=baseurl + encode
print encode
But this encode url is not working...what i found is the
the urlparse.urlencode takes the query as a dictionary and my value as itself is a dictionary. (nested)
if print values(working) and value(not working)
i find the single quotes are the one missing from value.
{'where': {'EntityId': 'srv-1499839282322'}}-----> not working
{'where': '{"EntityId": "srv-1499839282322"}'} ----> working
While running a code in python I want to update a dictionary value that is in config(global variable) I'm trying to do but i got the following error.
config.py
survey={'url':''}
runnigcode.py
url=config.__getattribute__(config.environment)
url['url']='myurl.com'
TypeError: "'module' object does not support item assignment"
in your runningcode.py, just import the variable as a method:
from config import survey
print survey
if you want to modify the value in the config.py, just use write method, like in this post: Edit configuration file through python
am getting the following post data in my django app
POST
Variable Value
csrfmiddlewaretoken u'LHM3nkrrrrrrrrrrrrrrrrrrrrrrrrrdd'
id u'{"docs":[],"dr":1, "id":4, "name":"Group", "proj":"/al/p1/proj/2/", "resource":"/al/p1/dgroup/4/","route":"group", "parent":null'
am trying to get the id value in variable id i.e "id":4 (the value 4). When I do request.POST.get('id')I get the whole json string. u'{"docs":[],"dr":1, "id":4, "name":"Group", "proj":"/al/p1/proj/2/", "resource":"/al/p1/dgroup/4/","route":"group", "parent":null' How can I get the "id" in the string?
The data you are sending is simply a json string.
You have to parse that string before you can access data within it. For this you can use Python's json module (it should be available if you're using Python 2.7).
import json
data = json.loads( request.POST.get('id') )
id = data["id"]
If you somehow don't have the json module, you can get the simplejson module.
For more details, refer this question : best way to deal with JSON in django
That's happening because id is string, not dict as it should be. Please provide your template and view code to find source of problem.