Jump to content

python experts


kevinUsa

Recommended Posts

import sqlite3
conn = sqlite3.connect('aaa.sqlite')
cur = conn.cursor()
 
cur.execute('DROP TABLE IF EXISTS Student')
cur.execute('DROP TABLE IF EXISTS Courses')
 
cur.execute('CREATE TABLE Student(name TEXT, Student_id INTEGER, course_id INTEGER)')
 
cur.execute('CREATE TABLE Courses (course_id INTEGER, Course_name TEXT)')
 
cur.execute('INSERT INTO Student (name, Student_id, course_id ) VALUES (?,?,?)', ( 'TOm', '20044844', '12345' ))
cur.execute('INSERT INTO Courses (course_id ,Course_name) VALUES (?,?)', ( '12345','maths' ))
cur.execute('SELECT name, Student_id FROM Student')
 
print('Student')
print('Courses')
 
conn.commit()
Link to comment
Share on other sites

I created a table than  manually inserted the values 

but when I try to print the table I am getting name of the table but not the values in the table 

Link to comment
Share on other sites

1 minute ago, r2d2 said:

Courses table select missing?

i want ot print values from student table only 

just started to learn python 

Link to comment
Share on other sites

Just now, kevinUsa said:

I created a table than  manually inserted the values 

but when I try to print the table I am getting name of the table but not the values in the table 

cur.execute('SELECT course_name, Course_id FROM Courses')
Link to comment
Share on other sites

Just now, Spartan said:
cur.execute('SELECT course_name, Course_id FROM Courses')

can you run on this let me me know what  results 

Link to comment
Share on other sites

1 minute ago, Spartan said:
cur.execute('SELECT course_name, Course_id FROM Courses')
import sqlite3
conn = sqlite3.connect('assingnment.sqlite')
cur = conn.cursor()
 
cur.execute('DROP TABLE IF EXISTS Student')
cur.execute('DROP TABLE IF EXISTS Courses')
 
cur.execute('CREATE TABLE Student(name TEXT, Student_id INTEGER, course_id INTEGER)')
 
cur.execute('CREATE TABLE Courses (course_id INTEGER, Course_name TEXT)')
 
cur.execute('INSERT INTO Student (name, Student_id, course_id ) VALUES (?,?,?)', ( 'TOm', '20044844', '12345' )),
cur.execute('INSERT INTO Student (name, Student_id, course_id ) VALUES (?,?,?)', ( 'TOm', '20044842', '12345' )),
cur.execute('INSERT INTO Student (name, Student_id, course_id ) VALUES (?,?,?)', ( 'TOm', '20044843', '12345' )),
cur.execute('INSERT INTO Student (name, Student_id, course_id ) VALUES (?,?,?)', ( 'TOm', '200448442', '12345' ))
 
cur.execute('INSERT INTO Courses (course_id ,Course_name) VALUES (?,?)', ( '12345','maths' )),
cur.execute('SELECT name, Student_id FROM Student')
cur.execute('SELECT course_name, Course_id FROM Courses')
 
print('Student')
print('Courses')
Link to comment
Share on other sites

1 minute ago, kevinUsa said:

can you run on this let me me know what  results 

it will result, but not exactly what u r looking for..

u might want to try inner join...its not python issue, its sql query issue..

calling sql experts.

 

select column1 from table1 
   inner join table2 on table1.column = table2.column
   where table2.columne=0
Link to comment
Share on other sites

Just now, Spartan said:

it will result, but not exactly what u r looking for..

u might want to try inner join...its not python issue, its sql query issue..

calling sql experts.

sql statement is correct I want just print the contents in students table 

when i try to do it I am getting  the name of the table but not the contents of the table

Link to comment
Share on other sites

1 minute ago, cosmopolitan said:

import sqlite3
conn = sqlite3.connect('aaa.sqlite')
cur = conn.cursor()
 
cur.execute('DROP TABLE IF EXISTS Student')
cur.execute('DROP TABLE IF EXISTS Courses')
 
cur.execute('CREATE TABLE Student(name TEXT, Student_id INTEGER, course_id INTEGER)')
 
cur.execute('CREATE TABLE Courses (course_id INTEGER, Course_name TEXT)')
 
cur.execute('INSERT INTO Student (name, Student_id, course_id ) VALUES (?,?,?)', ( 'TOm', '20044844', '12345' ))
cur.execute('INSERT INTO Courses (course_id ,Course_name) VALUES (?,?)', ( '12345','maths' ))
student_obj=cur.execute('SELECT name, Student_id FROM Student').fetchall()
course_obj=cur.execute('SELECT course_id, Course_name FROM Courses').fetchall()
 
print(student_obj)
print(course_obj)

got it thanks 

Link to comment
Share on other sites

4 minutes ago, kevinUsa said:

sql statement is correct I want just print the contents in students table 

when i try to do it I am getting  the name of the table but not the contents of the table

python lo print tharvatha qotes lo unte.. it just prints whatever is in quotes.. unless you give input using {} and format.

ex:

a=2
print('a')

print('a = {}'.format(a))

output :

a
a =2
Link to comment
Share on other sites

Just now, cosmopolitan said:

python lo print tharvatha qotes lo unte.. it just prints whatever is in quotes

ex:

a=2
print('a')

print('a = {}'.format(a))

output :


a
a =2

got it bro 

fetchall() y using here 

Link to comment
Share on other sites

7 minutes ago, kevinUsa said:

sql statement is correct I want just print the contents in students table 

when i try to do it I am getting  the name of the table but not the contents of the table

print(table) ('Student')

 

try this .. naku python radhu just google chesa first linke lo vunde .. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...