omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    how to check if my DBA is open

    Pythonista
    2
    4
    1856
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • sendog3c
      sendog3c last edited by

      • ***I am a very very beginner programmer and you will see it soon. I just started to create my app as part of a personal goal in Python.

      I read about SQLite3 and start to code with and example of creating a , everything goes fine until I run again my code and got an error showing me up that my dba is already created.

      My question is: There is a code or instruction that permit me to verify whether my dba file is open or not.

      code example:

      c = conn.cursor()

      Create table

      c.execute('''CREATE TABLE stocks
      (date text, trans text, symbol text, qty real, price real)''')

      Insert a row of data

      c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")

      Save (commit) the changes

      conn.commit()

      We can also close the connection if we are done with it.

      Just be sure any changes have been committed or they will be lost.

      conn.close()

      Thanks.


      cvp 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @sendog3c last edited by cvp

        @sendog3c First of all: welcome here...

        For the future, if you want to post your code in this forum, use the button </> above,
        it will help you to insert your code between two lines of three back quotes

        c = conn.cursor()
        
        # Create table
        c.execute('''CREATE TABLE IF NOT EXISTS stocks (date text, trans text, symbol text, qty real, price real)''')
        #c.execute('''CREATE TABLE stocks (date text, trans text, symbol text, qty real, price real)''')
         
        # Insert a row of data
        c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")
         
        # Save (commit) the changes
        conn.commit()
         
        # We can also close the connection if we are done with it.
        # Just be sure any changes have been committed or they will be lost.
        conn.close()
        

        Now, for your problem, use CREATE TABLE IF NOT EXISTS

        1 Reply Last reply Reply Quote 0
        • sendog3c
          sendog3c last edited by

          CVP:

          Thank you. Let me try your answer.

          1 Reply Last reply Reply Quote 0
          • sendog3c
            sendog3c last edited by

            CVP:

            Thank you again. This works.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            Powered by NodeBB Forums | Contributors