best way to rename a database field to given pattern if it already exixts? - mysql

I have a database table that contains id, filename, userId
id is unique identifier
filename should also be unique
table may contain >10000 records
When a user uploads a file it should be entered in database with given
rules:
If there is no record with same filename, it should be added as it is (Ex. foobar.pdf)
If there is record with same filename, it should be added as uploadedName(2).ext (foobar(2).pdf)
If there are n records with same base filename (foobar), it should be added as uploadedName(n+1).ext (foobar(20).pdf)
Now if foobar(2).pdf is uploaded, it should be added as foobar(2)(2).pdf & so on
This pattern needs to be followed because the file is already being
uploaded at client side using ajax before sending the details to
server and the file hosting service follows the above rules to name
the files.
My solution:
maintain a file that contains all the names and the number of times
it has occurred.
if a filename that exists in file is entered, increase occurrence count and new name is generated, else add to it to file
if the new name generated is in database, add it to file and generate new name

I would recommend you, since is an upload application, to do as follow
Create a column in your files table that store the original name of the file
Create another column to store a new generated name for the file, and this name would be some md5 or hash from the original plus the timestamp at the time of the upload, that ways you wouldn't have duplicated names.
Then when you upload your files you save then with this new name on the disk but show the original name to any application that requests it and if you need to download or stream it just get the saved hashed name from the database.

Related

how to delete all files in a single folder containing multiple folders that contains multiple files but only delete files that doesn't contain (2)?

I have converted thousands of sound files to a lower quality to reduce my project file size but instead of the converter overwriting the original files, it created duplicate files of the original ones with (2) on the end of each file name.
Now I want to delete the original files that doesn't have (2) on the end of the file name and then rename the duplicates with the original file names by simply removing the (2) on the end of each file name, but I don't want to do it one by one because it would take a very long time and my final defense is coming next week. Can anyone help?

How to reference a document in MySQL?

My database has a table within it, let's call it 'doclist'. I want the table to display a document name, the date associated with it, and a link to download this file. After researching it seems that the file (looking to include a PDF or Word doc) needs to be stored somewhere within htdocs, and I will need to reference the file location within one of the table fields. What type of field would that need to be: varchar, blob, or text? The only other tutorials I have seen including files are just dumping data, where I want to reference a file and allow its download. Any direction would be very helpful at this point.

Add PDF to database

I hope you can help me. In my database (supermarket) I have a table for orders and other for order_details and I would like to add the PDF of each invoice (associated to the order_number) to the database. Is this possible in mySQL? How can I do it?
You could store the PDF in an orders directory and insert the link into the db so when it is sent to the customer it would provide the link to the invoice
There are three possible ways I can think of to go about this.
Method 1.
Have a directory in which all the pdfs are stored. Give each PDF a name that is the order number found in the database.
Method 2.
Store the PDFs in a directory like method 1, but in your database store a file path to the PDF as a varchar. eg: /orderPdfs/124.pdf
Method 3.
Create a BLOB column in your order table and store the PDFs in this field.
Information on how to do this with PHP can be found in the answer to this post: How to store .pdf files into MySQL as BLOBs using PHP?

Importing excel file to access and set up columns field name

I'm having an access tool where I'm importing an excel file with table information. The system is creating a new table with this info with column fields (F1,F2,F3, etc.) and under it there is 10 lines with data and after that a table. I need the information from this table to be appended in another table in Access. I'm having the code and the append query, but sometimes some of the columns in excel file are change their places and this is a problem for my table 2. I would like to ask you is it possible somehow to change automatically the nameing of the column fields in the first table when I'm importing the info from the excel sheet.
Thank you in advance! - Here is a screenshot. The yellow one to go to the grey one.

Microsoft Dynamics Data Import Failing with Unresolved Lookup

I'm attempting to import a csv file in Microsoft Dynamics 2011. The file is an export of sales data from our website and I'm attempting to create new Account, Contact, Invoice, Order, Invoice Product and Order Product entities using this data. I'm using a multi-entity data map to do this.
When I attempt the import however, creating the invoice fails with the message "The lookup reference could not be resolved." in connection with the currency field. This field is a lookup tied to currency name which in all cases is "US Dollar". This is exactly the same name as the USD currency in our database, but for some reason the lookup refuses to assign it correctly. There are no other currencies in our database with this name.
Can anyone offer me an explanation as to why this is happening and any steps I can take to deal with it? Many thanks.
EDIT: More information about my problem.
It seems that problems occur when I try and perform a multiple entity import with duplicates. All the new entries I'm creating from this import is dependent on other new entries created with the same data. I'm creating accounts, then creating customers to assign to those accounts for example. If said accounts have duplicates already in the system, they are ignored. This is expected. However, when it comes to create the customer assigned to that account, the lookup fails with a "Duplicate lookup reference was found" error. This error occurs when there are more than one things in the database that fit the value I'm using with the lookup.
I'm not 100% sure, but I think it's counting both the account that was ignored and the original account that's already in the system when performing the lookup. Can anyone offer more insight on how I might work around this?
I don't know for sure if this is your issue but my guess it is.
In the import wizard you can select how he resolves your lookups.
i.e: when in your CSV file the column contains the name of the currency you will have to select the name field in your import wizard for the currency field.
In the screen here you can see then country is a lookup and the name of the country is the value of CSV file. It must match the field of the lookup. If I select "created on" It doesnt find the lookup. because no country exists where created on equals "Belgium". So you select the Name field of the lookup and that is how he will check your value in the CSV in the lookup field you defined.
1.Put records for each record type in a separate file.
2.Put records for different owners in a separate file.
3.Ensure that the first line of the file contains column headers and that they match the CRM attribute display names EXACTLY if you would like the import wizard to automatically* map the data. *Automatic mapping is required if you intend to import records that are related to another record (See #6). I HIGHLY RECOMMEND USING AUTOMATIC MAPPING EVEN IF YOU DO NOT DESIRE RELATED RECORDS.
4.Make sure the first column heading is not the name of a record type.
5.Make sure data exists in your source file for all CRM required fields (denoted with a red asterisk on the CRM form).
6.Make sure some data exists for each column. If a column has no data delete the column.
7.If your data should be related to another record (i.e. Adding Contacts to existing Accounts) make sure the column header matched the CRM lookup attribute display name. If you have two different attributes in CRM with the same display name you will receive an error (EX. if you created a custom State picklist value to replace the text version and keep the display name the same "Address 1: State/Province"). Also make sure the company name values will match a corresponding account record (i.e. contacts with a Parent Customer value of Microsoft Corp. will not be added to the CRM Account Microsoft) and that there are no duplicate Accounts in the system as the import will not be able to resolve the proper Parent Customer.
8.If you are importing into a drop-down list, ensure all the values exist within CRM and there are no typographic errors and consistent capitalization in the source file.
EDIT: It seems like indeed there are multiple records in your DB that can fit in that 1 lookup. it's normal that he gives an error on this as he can't resolve the lookup. The CRM doesn't know which record to take.
Also you might want to watch out if you are importing a M-N Relationship. You will have to do a M-N Relationship with code. The standard import wizard cannot handle Many-to-many relationships