insert data to mysql db table - mysql

I'm trying to insert data to my table in mysql db but again it send just null to my table instead of data.
this is my table fields:
MySQL [Alora]> desc car_list;
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| model_name | varchar(40) | YES | | NULL | |
| price | varchar(20) | YES | | NULL | |
| vehicle_milage | varchar(20) | YES | | NULL | |
+----------------+-------------+------+-----+---------+-------+
3 rows in set (0.009 sec)
and my code snippet:
def send_data (model_name, price, vehicle_milage):
cnx = mysql.connector.connect (user='root', password='root', host='localhost', database='Alora')
cursor = cnx.cursor ()
cursor.execute ('INSERT INTO car_list VALUES (model_name, price, vehicle_milage)')
cnx.commit ()
cursor = cnx.cursor ()
but after i run it my table is just look like this:
MySQL [Alora]> SELECT * FROM car_list;
+------------+-------+----------------+
| model_name | price | vehicle_milage |
+------------+-------+----------------+
| NULL | NULL | NULL |
| NULL | NULL | NULL |
+------------+-------+----------------+
2 rows in set (0.001 sec)
what should i do to send it correctly to my table?

I think your cursor.execute ('INSERT INTO car_list VALUES (model_name, price, vehicle_milage) is wrong.
You tell to insert empty rows. It has to look like this:
INSERT INTO car_list (model_name, price, vehicle_milage) VALUES ('BMW', 'thousend', 'test')

Related

I want an other table to be updated when I create an entry

I want start_date and start_time copied into latest_time and latest_date, while adding a new entry into my logbook. But I want dependency on logbook.logbook_index_id = logbook_index.id for all entries too.
mysql> describe logbook;
+-------------------------------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------------------+-----------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| logbook_index_id | int(10) unsigned | NO | | NULL | |
| start_date | date | NO | | NULL | |
| start_time | time | NO | | NULL | |
mysql> describe logbook_index;
+--------------------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+----------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| first_date | date | NO | | NULL | |
| first_time | time | NO | | NULL | |
| latest_date | date | NO | | NULL | |
| latest_time | time | NO | | NULL | |
+--------------------+----------------------+------+-----+---------+----------------+
atm I got this far ...
create trigger update_dates after insert on logbook
for each row update logbook_index
set latest_date = start_date where logbook_index.id = logbook_index_id;
I do it mostly wrong I bet. How does this work correctly and how do I get the time copied too ?
If I understood your question correctly:
For this I would suggest using a trigger
You can put an AFTER INSERT trigger on the table that you insert, inside the trigger you can put the update to the other table.
In order to access variables from the newly insert record, you need to do the following:
UPDATE logbook_index
SET latest_date = NEW.start_date
WHERE logbook_index.id = NEW.logbook_index_id;
Notice the keyword NEW that is used to access the newly insert record.
If you were using an AFTER UPDATE trigger, you could access the old values by using OLD
What you're searching for is a Trigger, a procedure that's automatically invoked in response to an event, in your case the insertion of a row in the logbook table.

Add rows into a mysql table from a .sql file

I have a question on How I can insert a .sql file into a MySQL table which already contains lot of data ?
My .sql file looks like (1200 rows) :
--
-- Descriptif plan comptable SYSCOHADA (utf-8)
--
INSERT INTO llx_accounting_system (rowid, pcg_version, fk_pays, label, active) VALUES (10,'SYSCOHADA', 49, 'Plan comptable Ouest-Africain', 1);
INSERT INTO llx_accounting_account (rowid, fk_pcg_version, pcg_type, pcg_subtype, account_number, account_parent, label, active) VALUES (15000,'SYSCOHADA','CAPITAUX','XXXXXX','1',0,"Capital",'1');
INSERT INTO llx_accounting_account (rowid, fk_pcg_version, pcg_type, pcg_subtype, account_number, account_parent, label, active) VALUES (15001,'SYSCOHADA','CAPITAUX','XXXXXX','101',15000,"Capital social",'1');
INSERT INTO llx_accounting_account (rowid, fk_pcg_version, pcg_type, pcg_subtype, account_number, account_parent, label, active) VALUES (15002,'SYSCOHADA','CAPITAUX','XXXXXX','1011',15001,"Capital souscrit, non appele);
My MySQL table looks like :
mysql> describe llx_accounting_account ;
+----------------+--------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+-------------------+-----------------------------+
| rowid | int(11) | NO | PRI | NULL | auto_increment |
| entity | int(11) | NO | | 1 | |
| datec | datetime | YES | | NULL | |
| tms | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| fk_pcg_version | varchar(32) | NO | MUL | NULL | |
| pcg_type | varchar(20) | NO | | NULL | |
| pcg_subtype | varchar(20) | NO | | NULL | |
| account_number | varchar(32) | NO | MUL | NULL | |
| account_parent | varchar(32) | YES | | NULL | |
| label | varchar(255) | NO | | NULL | |
| fk_user_author | int(11) | YES | | NULL | |
| fk_user_modif | int(11) | YES | | NULL | |
| active | tinyint(4) | NO | | 1 | |
+----------------+--------------+------+-----+-------------------+-----------------------------+
13 rows in set (0.00 sec)
My MySQL table is not empty. There is already data and I want to add my .sql file after my data table.
I didn't execute this command because I think it's false :
LOAD DATA LOCAL INFILE 'data_3.9.sql'
INTO TABLE llx_accounting_account
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n'
Do you have the solution ?
Thank you :)
----------------------------------------------------------------------------------
Solution :
With comments by #RakeshKumar and #PaulF, I found a way to solve my problem :
1) I deleted all rows where fk_pcg_version = 'SYSCOHADA':
delete from llx_accounting_account where fk_pcg_version = 'SYSCOHADA' ;
2) I imported the .sql file :
mysql -u root -p****** dolibarr < data_3.9.sql
3) I modified one information because account_number was 1 instead of 10 where rowid = 15000 :
UPDATE llx_accounting_account SET account_number = 10 WHERE rowid=15000 ;
Seems good :)
Thank you ;)
Use following way to import file
mysql -u username -p'password' dbname < filename.sql
Your import didn't work because you had already your same old SYSCOHADA rows in your table.
You can delete all rows where fk_pcg_version = 'SYSCOHADA' and import again your file corrected.
I had the same error importing data from a SQL file that was created from a mysqldump table output to file command. The reason was that the SQL file has the DROP TABLE IF EXISTS and CREATE TABLE statement at the start of the file.
Once this is removed, then it effectively appends the rows to the existing records.
Maybe this will help others.

Pupulate table with data in mysql

I have two databases
db1
db2
db1 has table controller below the description of controller table.
+-----------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+--------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| interface_name | varchar(255) | YES | | NULL | |
| store_number | int(11) | YES | | NULL | |
| file_seq_no | int(11) | YES | | NULL | |
| packet_seq_no | int(11) | YES | | NULL | |
| last_trans_start_date | date | YES | | NULL | |
| last_trans_end_date | date | YES | | NULL | |
| last_extract_datetime | datetime | YES | | NULL | |
| is_enabled | char(1) | YES | | NULL | |
+-----------------------+--------------+------+-----+---------+-------+
and same table description and additional column(date_opened) with different name available in database db2 with name store table.
My job is to write a query based on below logic.
Point 1 : Fetch all store_number, date_opened from db2.store.
Point 2 : Fetch all store_number from db1.controller table into list.
Point 3 : Filter new store_number fetched from db2.store not present in db1.controller.
For each of these new store_number
insert default initial values for new stores, file_seq_number and packet_seq_number will be set as zero.
last_transaction_start_date , last_transaction_end_date , last_extract_timestampwill be store creation date ie db2.store.date_opened
enabled will be set as 1
Main query would be
Insert into db1.controller table values( store_number, 0, 0,$date_opened,$date_opened,
$date_opened,1);
Can any one please help me to complete this sql query.
Thanks in advance
You can get filtered data by two ways.
With the help of subquery
Select db2.store .* from db2.store where db2.store.id not in(Select db1.controller.id from db1.controller)
Here all new data which is not present in db1.controller will be displayed. You can display same result With the help of join also.
SELECT db2.store.* FROM db2.store LEFT JOIN db1.controller ON db2.store.ID = db1.controller.ID WHERE db1.controller.ID IS NULL
You will get the data which is not present in db1.controller now iterate the data and execute your insert query.
Update
You can try below single query to add filtered records.
Insert into db1.controller (id,interface_name,store_number,file_seq_no,packet_seq_no,last_trans_start_date,last_trans_end_date,last_extract_datetime,is_enabled) SELECT db2.store.id, db2.store.interface_name, db2.store.store_number, 0, 0, db2.store.date_opened, db2.store.date_opened, db2.store.date_opened, db2.store.is_enabled FROM db2.store LEFT JOIN db1.controller ON db2.store.ID = db1.controller.ID WHERE db1.controller.ID IS NULL;
This something what i did
INSERT INTO db1.controller
(interface_name,store_number,file_seq_no,packet_seq_no,last_trans_start_date,last_trans_end_date,last_extract_datetime,is_enabled)
SELECT
'test',s.store_number,0,0,s.date_opened,s.date_opened,s.date_opened,'1'
FROM db2.store s where s.store_number not in (select store_number
from db1.controller);

how to port data to a table when both tables has different fields

mysql> desc oldtable;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| uid | int(11) | NO | PRI | NULL | auto_increment |
| active | char(1) | NO | | NULL | |
| field3 | char(256) | NO | | NULL | |
| field4 | char(256) | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
mysql> desc newtable;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| uid | int(11) | NO | PRI | NULL | auto_increment |
| active | tinyint(1) | NO | | 0 | |
| field5 | int(12) | NO | | 0 | |
| field6 | varchar(12) | NO | | 0 | |
| field7 | varchar(12) | NO | | 0 | |
+------------+--------------+------+-----+---------+----------------+
This is similar to my previous query change a field and port mysql table data via script ?
[I would like to port data (dump) from oldtable into newtable. One issue is, earlier the table used char(1) for active which stores value either 'Y' or 'N'. Now the newtable stores it as int either 1 or 0.
How can i fix this before porting data? Should I use shell script for such fix & porting ? Any sample scripts or tips :)]
But this question, How to achieve the same porting,If both tables has different no.of fields
like above?
The answer is similar to the previus question answer:
INSERT INTO newtable (uid, active, field5, field6, field7 )
SELECT uid,FIELD(active,'Y') as active, 0,'',''
FROM oldtable
Then update newTable with new fields values:
update newTable
set
field5 = (select someExpression from someTable5 t where t.uid=newTable.uid),
field6 = (select someExpression from someTable6 t where ...),
field7 = (select someExpression from someTable7 t where ...)
Also, you can define new fields as null allowed and leave this fields without value.

change a field and port mysql table data via script ?

mysql> desc oldtable;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| uid | int(11) | NO | PRI | NULL | auto_increment |
| active | char(1) | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
mysql> desc newtable;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| uid | int(11) | NO | PRI | NULL | auto_increment |
| active | tinyint(1) | NO | | 0 | |
+------------+--------------+------+-----+---------+----------------+
I would like to port data (dump) from oldtable into newtable. One issue is, earlier the table used char(1) for active which stores value either 'Y' or 'N'. Now the newtable stores it as int either 1 or 0.
How can i fix this before porting data? Should I use shell script for such fix & porting ?
Any sample scripts or tips :)
INSERT INTO newtable
SELECT uid,IF(active='Y',1,0) as active FROM oldtable
should do the trick
INSERT INTO newtable (uid, active)
SELECT uid, IF(active='Y', 1, 0) AS active
FROM oldtable
docs for this syntax here: http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-select-into-table.html
Joking version:
INSERT INTO newtable
SELECT uid,FIELD(active,'Y') as active
FROM oldtable