Automated Data Import Stored Procedure From an Excel File - mysql

I have this Excel file:
Based on this data, I want to create a stored procedure that will identify the correct meter, if it exists, and perform either an insert or update to the monthly data.
Here is the MonthlyData table:
I really have no idea where to get started on this. Sorry about the tables, I am new here and I cannot post pictures yet. Please copy the tables and paste it in Excel.
Thank you

It's probably easiest to create an SSIS package for this if you're going to do this repeatedly.
First, create two tables:
myDataRaw
myDataCleaned
With myDataRaw, you truncate the table and then upload the Excel file into that table using a data upload object.
Create the stored procedure to work with the raw data. I would truncate the myDataCleaned table and then do a INSERT ... SELECT to it, making the WHERE clause specific to finding the account meters that you're looking for. If there are a lot, you can create another table to hold the specific account meters you want to import and use it in your WHERE clause.
I hope that helps get you started.

Have you considered using MERGE Query? I have no idea what 'meter' in this context mean, but if its something that can be checked in database itself, then MERGE query will be the best solution to your problem.
http://www.jooq.org/doc/2.6/manual/sql-building/sql-statements/merge-statement/

Related

MySQL: create a different partition for each ID inside a trigger

I read about partitioning in MySQL, and would like to know how to use this in my situation:
I have a project where I store huge csv-files inside a database.
For this I have one table 'csvfiles' and another table 'csvdata'.
Queries are only made on one csv-file at the time, so my idea is to create a new partition in csvdata for each different primary key-value in csvfiles every time a csv-file is added.
Ideally this should be done automatically, inside a trigger would be great.
Could anybody tell me if this is a valid setup, and how this could be realized?

Shell file to import SQLITE into MYSQL

Ok, let me start off by saying that I'm don't have the slightest clue how to start off with this. I have an sqlite database. For simplicity lets just say that the table that I want to read is 'data' and data contains two fields, say (id, name). How could I go about creating a shell script to read the information from the 'data' sqlite table and insert it into a MYSQL table with the exact same table structure? I realise that it would be simpler to just insert the data into MYSQL to begin with and cut out the sqlite step all together, but this is not possible (unfortunately). I really appreciate any help!
http://web.archive.org/web/20121018070614/http://sqlite.phxsoftware.com/forums/p/941/4725.aspx
[corrected dead link. there is a C# script which accomplishes the objective]

Create MySQL table from xls spreadsheet

I wonder if there is a (native) possibility to create a MySQL table from an .xls or .xlsx spreadsheet. Note that I do not want to import a file into an existing table with LOAD DATA INFILE or INSERT INTO, but to create the table from scratch. i.e using the header as columns (with some default field type e.g. INT) and then insert the data in one step.
So far I used a python script to build a create statement and imported the file afterwards, but somehow I feel clumsy with that approach.
There is no native MySQL tool that does this, but the MySQL PROCEDURE ANALYSE might help you suggest the correct column types.
With a VB Script you could do that. At my client we have a script which takes the worksheet name, the heading names and the field formats and generates a SQL script containing a CREATE TABLE and a the INSERT INTO statements. We use Oracle but mySQL is the same principle.
Of course you could do it even more sophisticated by accessing mySQL from Excel by ODBC and post the CREATE TABLE and INSERT INTO statements that way.
I cannot provide you with the script as it is the belonging of my client but I can answer your questions on how to write such a script if you want to write one.

Is there any way to automatically create a trigger on creation of new table in MySQL?

Is there any way to automatically create a trigger on creation of new table in MySQL?
As I've pointed out in your other question, I think a process and security review is in order here. It's an audited database, so nobody (especially third-party service providers) should be creating tables in your database without your knowledge.
The issue you've got is, as well as the new table being created, you will also need to have another table created to store the audited/changed records, which will have an identical structure as the original table with possibly a time/date and user column. If a third-party provider is creating this table, they won't know to create the auditing table, therefore even if you could generate your triggers dynamically, they wouldn't work.
It's impossible to create a single table that will hold all changes record for all other tables in your database because the structure between tables inevitably differs.
Therefore: make all change requests (e.g. providers wants to create TableX, they submit a change request (including the SQL script) explaining the reason for the change) to yourself and/or your team.
You execute the SQL on a test copy of your database, and use the same structure to create another table to hold the modified records.
You then create and test the necessary triggers, generate a new SQL script to create the two tables and your triggers and execute that on your live database. You give your provider permissions to use the new table and away they go.
Everyone's happy. Yes, it may take a little while longer, and yes you'll have more work to do, but that's a hell of a lot less work than is required to try and parse query logs to re-create records that have already been changed/deleted, or parse the binary log and keep up-to-date with every change, and modify your code when the format of the log file changes etc etc.

Saving MySQL records as a series of Insert Statements

Having carefully and painfully manually populated/manipulated a series of records in a table, I want to retain them for reuse. As the table is rewritten daily I'd like to save JUST these particular records as a series of "inserts". The only way I know how to do this is to dump the whole table as sql using a GUI eg sqlyog.
But is there any quicker/better way to do this?
would mysqldump help ? (it's not a GUI)
edit : note that you can save only part of a table using this tool. since it's command line, you can automate the task easily.
Create a copy of your table with a meaningful name and copy using a INSERT the records your interested in.
Doing it this way gives the most flexibility should you need to copy them back/compare them.
Providing you still have your clean manually populated table available. Copy it into another table:
CREATE TABLE mytable_backup SELECT * FROM mytable;
Then you can re-introduce these to your daily rebuild table using a similar method:
INSERT INTO mytable SELECT * FROM mytable_backup;
Does this help?