Home > Mobile >  How to only get first column first row value from sqlite?
How to only get first column first row value from sqlite?

Time:03-11

I wanna to know that how to only get first column first row value from sqlite and not getting all column value at one time. For example: data image

So in this image, Thee first row's value which is 'red', 'blue', '', '', '', '', ''. I want to only get the value red. Is there anyway to get only this value? I have tried with

    con = sqlite3.connect(database=r'ims.db')
    cur = con.cursor()
    cur.execute("Select occolor1, occolor2, occolor3, occolor4, occolor5, occolor6, occolor7 from ordercart")
    row= cur.fetchall()
    for a,b,c,d,e,f,g in row:
        print(a)

but this will show also the the second column value which is red and black. Anyone can help?

CodePudding user response:

Well assuming there be some column which provides an ordering you want, you may use a LIMIT query and then just select the first column:

con = sqlite3.connect(database=r'ims.db')
cur = con.cursor()
cur.execute("SELECT occolor1 FROM ordercart ORDER BY id LIMIT 1")
row = cur.fetchone()
print(row)

CodePudding user response:

SELECT 
   occolor1 
FROM 
   ordercart 
ORDER BY
   occolor1
LIMIT 1

CodePudding user response:

con = sqlite3.connect(database=r'ims.db')
cur = con.cursor()
query = "Select occolor1 from ordercart"
df = pd.read_sql(query, con)
df

This will give you the first column and put it into a df for further use

  • Related