what is the purpose of establishing a datasource - python

I am building a web application in Flask.
We have opened up the database window of PyCharm and established a data source to a SQL server database.
My question is what does establishing a data source do?
Does is remove the need to connect to a database manually?, like for example
db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
If the answer is yes it does remove the need to set
updb = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
then how can you access the data in the database, and establish a cursor object?

The JetBrains IDEs such as PyCharm or IntelliJ have a database browser, basically productionalized as it's own IDE called DataGrip, but that's besides the point.
Fact is, no, that doesn't replace the need for code, and you could have zero code and make a database connection, or entirely code and never touch the database window, ever (because you don't need PyCharm to write said code).
So, they are separate things, just like how "SQL Server" means something completely different from "MySQL" (e.g. you might need a different library)

Related

how to create a multiple connection with advantage database server at same time?

I am trying to integrate alongside an existing application that uses ADS as its database.
When i connect my integration app using the code below it connects fine until i try and run the original application at the same time. It seems to only allow one connection, my application seems to hold the connection and block all others. Yet i can have multiple instances of the original application running conncurrently with no issue. Which leads me to believe that its the way in which i am trying to correct from my c# app. The error im getting when the original app is open and i then try to connect with my integration app is "The Advantage Data Dictionary cannot be opened. axServerConnect" .
Error 7077: The Advantage Data Dictionary cannot be opened. axServerConnect
Anyone any suggestions? How to create a multiple connection at same time?
Python code:
conn = adsdb.connect(DataSource=str(dbpath[0]), ServerType='local',
UserID = config.ADS_USERNAME, password=config.ADS_PASS)
According to this page in ADS documentations, you can use connection pooling by providing pooling=True to your client connection arguments.
I think using this approach, you will be able to open multiple connections at the same time.
Edit
After checking adsdb python script, I think it does not support connection pooling. You probably be able to set that connection pooling in your C# application.

web2py database always locked when trying to update from code

Trying to simply update existing row in database (running on web2py),
but always getting database locked error..
Error message:
<class 'sqlite3.OperationalError'> database is locked
My setup
in models/db.py I create database and it works when using database administration (can insert, update using the web interface)
db.define_table('mytest', Field('name', 'string'))
I have added 1 row to mytest, using the web interface (so its not empty)
in controllers/test.py i have simple code to get first item and try to update the value, there it fails (I open the page is browser and it gives the internal error, with link to error log)
def index():
# connect
db = DAL('sqlite://storage.sqlite',pool_size=10,auto_import=True)
# get first record
record = db(db.mytest).select().first()
# try to update it.. database locked error here
record.update_record(name="asdfg")
# just in case needed?
db.commit()
db.close()
return "test"
Software
WinPython2.7
Running win2py.py (2.14.6) manually using Spyder ide
windows8
What i've tried so far
Different DAL settings, poolsize, without autoimport..
Close all web2py admin tools/tabs
Create new database
Restart web2py
Restart pc
Error log: http://pastebin.com/2WMWypt6
Current workaround:
- Create New Application, exact same code seems to work there
Solution was: by #GauravVichare
- Remove this line from controller (its already defined in db.py)
db = DAL('sqlite://storage.sqlite',pool_size=10,auto_import=True)
Check Whether there is no other connection (to sqlite db) open on your machine, if web2py shell is open, close it.
Check DAL is defined only once or not. Define DAL only in models/db.py, no need to define it again in controller.
Every variable defined in models is visible in controllers.
You must have defined DAL in models/db.py and you are defining once again in controller, so you have two connection open for SQLite db. Thats why you are getting error 'database is locked'.
My Suggestion is
1.First of all save the code when u make some changes.
2.Aftr saving u r new code try to reload web2py.exe
then run web2py so that u wont get Databaselocked error.
3.Dont ever create tables in Sqlite database before.
4.once u start running web2py and starts server and when ever u enter data into forms it automatically creates the tablesin sqlite database.
Try using myRecord instead of Record, since it may be a reserved word.
I know User has given people issues in web2py. I would just tend to stay away from very generic aliases.
Otherwise, is there anything currently in the db? If it is empty you would receive and error.
It might be better to :
myRecord = record = db(db.mytest).select().first()
if myRecord:
myRecord.update_record(name="asdfg")
else:
[insert statement here]

Select a single active database connection when starting Plone

I have a Plone 4 site which uses an additional Postgres database via a Z Psycopg 2 Database Connection object. Since the ZODB is sometimes replicated for testing and development purposes, there are a few fellow database connection objects, in a project_suffix naming scheme; this way, I can select one of the existing database adapters via buildout configuraton script.
However, I noticed that all existing database connection objects are apparently opened when Plone starts up. I don't know whether this is a real problem (e.g. when applying changes to the schema of the database of another instance), but I'd rather have Plone open only the single database which is actually used. How can I achieve this?
(Plone 4.2.4, Postgres 9.1.9, psycopg2 2.5.1, Debian Linux)
Update:
I added some code to the __init__.py of my product, which looks roughly like this:
from Shared.DC.ZRDB.Connection import Connection
...
dbname = env['DATABASE']
db = None
for id, obj in portalfolder.objectItems():
if isinstance(obj, Connection):
if id == dbname:
db = obj
else:
print 'before:', obj._v_connected
obj._v_database_connection.close()
print 'after: ', obj._v_connected
However, this seems not to work; there are no exceptions I'm aware of, but for both before and after, I get a timestamp, and when looking in the ZMI afterwards, the connections seem to be open.
Any ideas, please?

Is there a way to check for new objects/entries in a Django database?

I am writing a Python server in Tornado which works with HTML5 WebSockets. My server works by creating a connection with the client browser through JavaScript. Once a connection is created, it stays open until the browser (or the server closes it). I need to periodically check if one of my models has changed or if the database has updated.
Here's code example to demonstrate what I mean:
>>> mymodels = MyModel.objects.all()
>>> len(mymodels)
150
>>> # Some stuff happens on the client and the model is changed, one more entry is added
>>> mymodels = MyModel.objects.all()
>>> len(mymodels)
151
This all happens within a server application where the changes to the model will occur within one "session" of the server script running. Is there anyway I can check for new objects or refresh my Django database?
An example of what I mean if it still isn't clear: Let's say I have a model called MyModel. When the server script is first run, it has 150 different entries or database rows. I establish a WebSocket connection with my server from my client and request that I be updated whenever a new change occurs. Somewhere else in my client, some other user does something that creates a new row in my database for the MyModel class. My server, while still keeping the same connection that it has to the original client already, needs to be able to detect that change without stopping its execution.
Checking periodically isn't the problem, its actually making sure that the Django database API is aware of the newly added information. Is there anyway I can ensure that that happens? The originally posted example code does not actually work. The length of MyModel.objects.all() is still 150 no matter how many items I add to the model. If I restart my Django shell, it updates the count.
Some other things I have tried:
Reloading the models module using the built-in reload() function.
Filtering the model for a certain set of MyModel
Using raw SQL queries to both select everything and filter based on certain conditions
All of these methods keep returning the same number of MyModel objects no matter how many changes I make to the database. Interestingly enough, running the raw SQL in MySQL Workbench produces the expected results.
I FIGURED IT OUT!
The simplest way to force Django to update its database reference is to close the database connection. Django will automatically create a new one as it needs to.
django.db.close_connection()
If you have changes that need to be committed before you close the connection, this will accomplish the same as above, but keep the changes that you have made. (i.e. you will not need to close the connection as this refreshes the database anyway)
django.db.connection._commit()
Thanks for your comments and have a nice day!
If it's all within one "session" of the server-side script running, maybe you've got the whole thing running in one DB transaction (which would mean that nothing else could see it) - although the fact that you can see them incrementally in Workbench suggests not. Having said that, it's worth checking out what you're doing with transactions.
Also, have you read this to make sure that Django is doing what you think it's doing?

Python ejabberd Auth Script not responding to changes in Database

I have an authentication script in ejabberd (XMPP server) that based off of THIS LINK
I have slightly modified the script so that instead of setting the variable out, it just returns true or false.
I'm using Ubuntu, MySQL, ejabberd, and Python.
I can authenticate all the records that are already on the database. But, when I add or remove records (I do this through phpMyAdmin), the script doesn't seem to know that the database has changed (I remove a user in phpMyAdmin and it still authenticates the user). The only time when the script recognizes the new records is when I restart or force-reload the ejabberd server. I've already been told its not a mySQL caching problem. I made sure I turned off external authentication caching for ejabberd.
That's all I can think of right now. I'll add more information if I can think of it. Any help is appreciated. I have no idea what is going on.
Addition: I turned on the MySQL logs, and all the queries there so there is not skipping queries.
I managed to fix this problem by changing the database engine back to MYISAM rather than INNODB. But I would like to know if this can be fixed for INNODB.
Edit: to fix it in innodb, set autocommit to true

Categories