wiley_pastebin icon

1.3 SQL1

wiley_pastebin | PRO | 11/18/19 08:17:33 AM UTC | 0 ⭐ | 7882 👁️ | Never ⏰ | []
SQL |

850 B

|

None

|

0 👍

/

0 👎

CONNECT()
import sqlite3
# CREATE a DATABASE IN RAM
db = sqlite3.CONNECT(':memory:')
# CREATE/OPEN a file called mydb.
db = sqlite3.CONNECT('data/mydb')
 
cursor AND commit()
# GET a cursor object
cursor = db.cursor()
#execute the query
cursor.EXECUTE('''CREATE TABLE users(id INTEGER PRIMARY KEY, name TEXT, email TEXT)''')
#commit it TO the DATABASE
db.commit()
 
ROLLBACK()
#rollback the execution
db.ROLLBACK()
 
fetchone()
# obtains SOME ROWS FROM a TABLE
cursor.EXECUTE('''SELECT name,email FROM users''')
#retrieve the FIRST ROW
user1 = cursor.fetchone()
#Print the FIRST COLUMN retrieved(USER's name)
print(user1[0])
 
fetchall()
# obtains some rows from a table
cursor.execute('''SELECT name, email FROM users''')
#retrieve all the rows
all_rows = cursor.fetchall()
for row in all_rows:
print('{0} : {1}'.format(row[0], row[1]))

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎