Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sqlite CONCAT columns
#1
How should I create a column by concat First Name and LastName in Sqlite (python environment)?
I getting a syntax error when i defining "as" below

conn_dB = sqlite3.connect('EmployeeDB.db')
c= conn_dB.cursor()
c.execute(SELECT *CONCAT( "FirstName" ,' ', "LastName") as FullName FROM Employee)
conn_dB.commit()
c.close()
Reply
#2
The SQL statement you pass to execute needs to be a string.
Reply
#3
c.execute("SELECT FirstName || ' ' || LastName as FullName from Employee")
It's not clear what you want - display concatenated names or insert a new column in a table, which you should not do given that you already have first name and last name as columns.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(Mar-22-2020, 08:24 AM)buran Wrote:
c.execute("SELECT FirstName || ' ' || LastName as FullName from Employee")
It's not clear what you want - display concatenated names or insert a new column in a table, which you should not do given that you already have first name and last name as columns.
hi buran,

thx for the code, it is working but DB does not update it. how to update in DB?
Reply
#5
This code does not update the database, it's a SELECT query.
If you have FirstName and LastName it's generally against DB design principles to have also FullName as column. Keep atomic data. Note that SQLite3 does not support computed columns if you are after that.
You can create a view, if you want to have a full name and run queries against the view.
If after all you want to do it and create a new column in the table, then you need to create a column FullName and update its values from the other two columns:
import sqlite3
with sqlite3.connect('test.db') as db:
    curr = db.cursor()
    curr.execute("ALTER TABLE Employee ADD COLUMN FullName TEXT;")
    curr.execute("UPDATE Employee SET FullName = FirstName || ' ' || LastName;")
    db.commit()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create SQLite columns from a list or tuple? snakes 6 2,179 May-04-2021, 12:06 PM
Last Post: snakes
  pd.concat Problem WiPi 1 743 May-27-2020, 07:42 AM
Last Post: WiPi
  Concat multiple Excel sheets with exclusion alessandrotk 1 1,572 Jan-10-2020, 04:43 AM
Last Post: sandeep_ganga
  SQLite DB integration duplicate columns rachitmishra25 1 3,894 Oct-27-2017, 11:20 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020