Python Forum
Sqlite CONCAT columns - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Sqlite CONCAT columns (/thread-25167.html)



Sqlite CONCAT columns - issac_n - Mar-22-2020

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()



RE: Sqlite CONCAT columns - ndc85430 - Mar-22-2020

The SQL statement you pass to execute needs to be a string.


RE: Sqlite CONCAT columns - buran - Mar-22-2020

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.


RE: Sqlite CONCAT columns - issac_n - Mar-22-2020

(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?


RE: Sqlite CONCAT columns - buran - Mar-22-2020

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()