SQLite3 and lastrowid
I am fairly new to Python (no development experience) and brand new to using sqlite through Python. With that said, I created a database with two tables. The first has about 30,000 rows of static data. The second has 9 rows of static data. Before I added the second table I could simply run 'print(cursor.lastrowid)' and it would give me the id number. However, with two tables I am unable to do this. Does anyone know if there is a way to reference one table or another to get lastrowid? Thanks!!! -- http://mail.python.org/mailman/listinfo/python-list
Re: SQLite3 and lastrowid
On Nov 16, 12:54 pm, Ian wrote:
> On Nov 16, 1:00 pm, fuglyducky wrote:
>
> > Before I added the second table I could simply run
> > 'print(cursor.lastrowid)' and it would give me the id number. However,
> > with two tables I am unable to do this.
>
> It would help if you would show the code where you're trying to do
> this. Without your actual code to look at, we can't tell you why it
> doesn't work.
>
> > Does anyone know if there is a way to reference one table or another
> > to get lastrowid?
>
> cursor.lastrowid is always in reference to the last query executed by
> the cursor, not in reference to a table. If you don't capture the
> value, and then you execute another query on the same cursor, the
> previous value of cursor.lastrowid no longer exists. If you need it
> now, then either you should have captured it when you had the chance,
> or you should not have executed another query on the same cursor.
>
> But perhaps this is not what you're actually trying to do. I can't
> tell, because I haven't seen the code.
>
> Cheers,
> Ian
Thanks for the input. Sorry...I should have included the code...it's
just a simple query...
#
import sqlite3
import random
db_connect = sqlite3.connect('test.db')
cursor = db_connect.cursor()
print(cursor.lastrowid)
# Choose random index from DB - need to understand lastrowid
#row_count = cursor.lastrowid
#random_row = random.randrange(0, row_count)
cursor.execute("SELECT * FROM table1 WHERE id = 2002")
print(cursor.fetchmany())
#for row in cursor:
# print(row)
db_connect.commit()
cursor.close()
#
--
http://mail.python.org/mailman/listinfo/python-list
Re: SQLite3 and lastrowid
On Nov 16, 1:52 pm, Ian wrote:
> On Nov 16, 2:08 pm, fuglyducky wrote:
>
> > db_connect = sqlite3.connect('test.db')
> > cursor = db_connect.cursor()
>
> > print(cursor.lastrowid)
>
> At this point you haven't executed a query yet, so there is no
> meaningful value that cursor.lastrowid can take.
>
> > # Choose random index from DB - need to understand lastrowid
> > #row_count = cursor.lastrowid
> > #random_row = random.randrange(0, row_count)
>
> This is wrong. The lastrowid is not the number of rows in the table.
> It's the row-id of the row that was inserted (if any) by the last
> query executed by the cursor. In the case of sqlite3, I think the row-
> id is just the primary key if it's an integer (but in general the row-
> id is database-dependent), so you *might* be able to get away with it
> if you always let it autoincrement when inserting, never delete any
> rows, and never change their primary keys. But it's unreliable and
> only available immediately after an insert, so don't do it that way.
>
> The proper way to get the number of rows is to use the COUNT aggregate
> function, e.g., "SELECT COUNT(*) FROM TABLE1", which will return a
> single row with a single column containing the number of rows in
> table1.
>
> Cheers,
> Ian
Ahhh...great...thanks for the info! I'll do the row count then!!!
--
http://mail.python.org/mailman/listinfo/python-list
Newbie: Python 3 and MySQL???
I am brand new Python and need to connect to a MySQL DB. The books I have been using are all Python 3 so I'd like to stick with what I know. Does anyone know if there is a library out there for connecting to MySQL with Python 3? If not, does anyone have any info on when MySQLdb may be ported? Thanks!!! -- http://mail.python.org/mailman/listinfo/python-list
Python 3 and setuptools
I am trying to install a library that requires setuptools. Unfortunately, setuptools isn't available for Python 3 yet. Is this correct? Any idea when it may be available OR if there is a different tool/method of getting setuptools installed for Python 3? Thanks!!! -- http://mail.python.org/mailman/listinfo/python-list
Confused: Newbie Function Calls
I am a complete newbie to Python (and programming in general) and I have no idea what I'm missing. Below is a script that I am trying to work with and I cannot get it to work. When I call the final print function, nothing prints. However, if I print within the individual functions, I get the appropriate printout. Am I missing something??? Thanks in advance # Global variable sample_string = "" def gen_header(sample_string): HEADER = """ mymultilinestringhere """ sample_string += HEADER return sample_string def gen_nia(sample_string): NIA = """ anothermultilinestringhere """ sample_string += NIA return sample_string gen_header(sample_string) gen_nia(sample_string) print(sample_string) -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused: Newbie Function Calls
On Aug 11, 9:31 am, Pinku Surana wrote: > On Aug 11, 12:07 pm, fuglyducky wrote: > > > > > I am a complete newbie to Python (and programming in general) and I > > have no idea what I'm missing. Below is a script that I am trying to > > work with and I cannot get it to work. When I call the final print > > function, nothing prints. However, if I print within the individual > > functions, I get the appropriate printout. > > > Am I missing something??? Thanks in advance > > > > > # Global variable > > sample_string = "" > > > def gen_header(sample_string): > > HEADER = """ > > mymultilinestringhere > > """ > > > sample_string += HEADER > > return sample_string > > > def gen_nia(sample_string): > > NIA = """ > > anothermultilinestringhere > > """ > > > sample_string += NIA > > return sample_string > > > gen_header(sample_string) > > gen_nia(sample_string) > > > print(sample_string) > > There are 2 problems with your program. > > (1) If you want to use a global variable in a function, you have to > add the line "global sample_string" to the beginning of that > function. > > (2) Once you do (1), you will get an error because you've got > sample_string as a global and a function parameter. Which one do you > want to use in the function? You should change the name of the > parameter to "sample" to solve that confusion. > > Here's the result, which works for me: > > sample_string = "" > def gen_header(sample): > global sample_string > HEADER = """ > mymultilinestringhere > """ > sample_string = sample + HEADER > return sample_string > def gen_nia(sample): > global sample_string > NIA = """ > anothermultilinestringhere > """ > sample_string = sample + NIA > return sample_string > gen_header(sample_string) > gen_nia(sample_string) > print(sample_string) Thanks! That did the trick. I am a bit confused though. I tried to follow a sample in a book (which works) where I didn't have to 1) pass the global variable as a parameter into the function, 2) did not have to define the global variable within the function. I apologize if this is a super stupid question but if it is global, why do I have to pass it into the function? Shouldn't the global variable be accessible from anywhere??? -- http://mail.python.org/mailman/listinfo/python-list
re.sub and variables
I have a function that I am attempting to call from another file. I am
attempting to replace a string using re.sub with another string. The
problem is that the second string is a variable. When I get the
output, it shows the variable name rather than the value. Is there any
way to pass a variable into a regex?
If not, is there any other way to do this? I need to be able to dump
the variable value into the replacement string.
For what it's worth this is an XML file so I'm not afraid to use some
sort of XML library but they look fairly complicated for a newbie like
me.
Also, this is py3.1.2 is that makes any difference.
Thanks!!!
#
import random
import re
import datetime
def pop_time(some_string, start_time):
global that_string
rand_time = random.randint(0, 30)
delta_time = datetime.timedelta(seconds=rand_time)
for line in some_string:
end_time = delta_time + start_time
new_string = re.sub("thisstring", "thisstring\\end_time",
some_string)
start_time = end_time
return new_string
--
http://mail.python.org/mailman/listinfo/python-list
Re: re.sub and variables
On Aug 12, 2:06 pm, fuglyducky wrote:
> I have a function that I am attempting to call from another file. I am
> attempting to replace a string using re.sub with another string. The
> problem is that the second string is a variable. When I get the
> output, it shows the variable name rather than the value. Is there any
> way to pass a variable into a regex?
>
> If not, is there any other way to do this? I need to be able to dump
> the variable value into the replacement string.
>
> For what it's worth this is an XML file so I'm not afraid to use some
> sort of XML library but they look fairly complicated for a newbie like
> me.
>
> Also, this is py3.1.2 is that makes any difference.
>
> Thanks!!!
>
> #
>
> import random
> import re
> import datetime
>
> def pop_time(some_string, start_time):
> global that_string
>
> rand_time = random.randint(0, 30)
> delta_time = datetime.timedelta(seconds=rand_time)
>
> for line in some_string:
> end_time = delta_time + start_time
> new_string = re.sub("thisstring", "thisstring\\end_time",
> some_string)
> start_time = end_time
>
> return new_string
Disregard...I finally figured out how to use string.replace. That
appears to work perfectly. Still...if anyone happens to know about
passing a variable into a regex that would be great.
--
http://mail.python.org/mailman/listinfo/python-list
Newbie: strftime object error message
I am trying to call a function with a couple additional parameters.
Unfortunately, for whatever reason I am unable to get this to work. I
am assuming that line is not passing correctly but I don't understand
why???
I can't share all of the code because it has IP in it but below are
the pertinent snippets.
Thanks in advance!
##
PY: 3.1.2
FUNCTION CALL:
text_file = open(file_name, "r")
for line in text_file:
new_foo = pop_time(current_time, line)
# current_time = datetime.datetime.now()
FUNCTION:
def pop_time(line, cur_time):
global new_string
global current_time
# Generate random time between 0 and 30 seconds
# Changes the time delta every time function is run
rand_time = random.randint(0, 30)
delta_time = datetime.timedelta(seconds=rand_time)
# Sets the time format for string output
format_time = cur_time.strftime("%Y-%m-%d %H:%M:%S")
ERROR MSG:
format_time = cur_time.strftime("%Y-%m-%d %H:%M:%S")
AttributeError: 'str' object has no attribute 'strftime'
NOTE: current_time prints within the function, line does not
--
http://mail.python.org/mailman/listinfo/python-list
Re: Newbie: strftime object error message
On Aug 16, 10:27 am, Mark Lawrence wrote:
> On 16/08/2010 17:47, fuglyducky wrote:
>
>
>
> > I am trying to call a function with a couple additional parameters.
> > Unfortunately, for whatever reason I am unable to get this to work. I
> > am assuming that line is not passing correctly but I don't understand
> > why???
>
> > I can't share all of the code because it has IP in it but below are
> > the pertinent snippets.
>
> > Thanks in advance!
>
> > ##
>
> > PY: 3.1.2
>
> > FUNCTION CALL:
> > text_file = open(file_name, "r")
> > for line in text_file:
> > new_foo = pop_time(current_time, line)
> > # current_time = datetime.datetime.now()
>
> > FUNCTION:
> > def pop_time(line, cur_time):
>
> Look at the two arguments to pop_time, when you make the actual call
> they are swapped.
>
>
>
> > global new_string
> > global current_time
>
> > # Generate random time between 0 and 30 seconds
> > # Changes the time delta every time function is run
> > rand_time = random.randint(0, 30)
> > delta_time = datetime.timedelta(seconds=rand_time)
>
> > # Sets the time format for string output
> > format_time = cur_time.strftime("%Y-%m-%d %H:%M:%S")
>
> > ERROR MSG:
> > format_time = cur_time.strftime("%Y-%m-%d %H:%M:%S")
> > AttributeError: 'str' object has no attribute 'strftime'
>
> > NOTE: current_time prints within the function, line does not
>
> HTH.
>
> Mark Lawrence.
Oh jeez!!! Thanks! Man...looking at this stuff for so long it's easy
to miss the obvious!!! Thanks and sorry for the waste of time.
--
http://mail.python.org/mailman/listinfo/python-list
Ugh! Python 3.1.x and MySQL
Most of the python books coming out now are Py3K. I just started programming and have a need to access a MySQL database. I would like to use Python to do this. Unfortunately, I cannot find anyone that has created anything that allows my to do this. I've tried installing an ODBC driver and using sqlalchemy, oursql, and a few other things with no luck. So...just wondering if anyone is aware of any libraries/modules that I can use to connect to a MySQL DB using Python 3.1.x? Ideally, I'd like to be able to this from both x86 and x64 systems (if that makes any difference). Thanks for any input you may have!!! -- http://mail.python.org/mailman/listinfo/python-list
Re: Looping through files in a directory
On Nov 10, 4:14 pm, James Mills wrote: > On Thu, Nov 11, 2010 at 8:46 AM, Matty Sarro wrote: > > Short story - I have a few thousand files in a directory I need to parse > > through. Is there a simple way to loop through files? I'd like to avoid > > writing a python script that can parse 1 file, and have to call it a few > > thousand times from a bash script. Any input or pointers to functions that'd > > help would be very much appreciated. Thanks! > > os.walk or os.listdir > > cheers > James > > -- > -- James Mills > -- > -- "Problems are solved by method" I was able to do something similar like this... rootPath = 'T:/' pattern = '*.xml' for root, dirs, files in os.walk(rootPath): for filename in fnmatch.filter(files, pattern): full_path = (os.path.join(root, filename)) print(full_path) -- http://mail.python.org/mailman/listinfo/python-list
