Listado de la etiqueta: Rename


A variant of ALTER TABLE is supported by SQLite. A preexisting table can be modified, have a field retitled, have a field inserted into it, or have a field eliminated from it using SQLite’s ALTER TABLE statement. The table’s column name is renamed a new name with the RENAME COLUMN command. We can only modify a table inside the same dataset using this operation. Whereas if the table changes to include triggers or indices, these are kept just after the change. Therefore, we have decided to cover the topic of renaming columns of an SQLite table in this guide.

This article begins with the launch of the shell application first in Ubuntu 20.04 system. Using the Ctrl+Alt+T, we have opened it and started to update our system with the apt update instruction. After adding the password for the currently logged-in user, the updating process has started and been completed.

After updating the system, we have to ensure that our system and its internal packages are upgraded to the newest version. So, we have been upgrading its packages using the apt upgrade instruction, as displayed below:

After successfully updating and upgrading our system, we will be moving toward launching the SQLite database within the terminal shell. You have to utilize the single keyword “sqlite3” to start it. The SQLite shell will be launched on our screen, and we can use it for querying data.

After opening it, we have listed the tables of a database with the “.tables” instruction and found there are no tables so far in the database.

To rename a column, we must have a table in the database. Therefore, we have been making a table titled “Test” within our current SQLite database with the CREATE TABLE instruction. Within this table, we will have two columns ID and Name. The ID column will contain an integer type value representing the primary key of a table that must not be NULL. The Name column will be of Text type and must not be NULL as well. Now, we have a “test” table within the list of tables as per the “.tables” instruction. Selecting the records of a Test table, we have found that it is empty and needs some records to be inserted within it.

Therefore, we have inserted five records within the ID and Name column of a table “Test” using the INSERT INTO instruction using the VALUES keyword followed by the records to be inserted. The five records are unique and have no duplicate values. After inserting the records, we have been checking the table records with the help of a SELECT instruction followed by the asterisk “*” character and the table name “Test”. This query returns all the five records of this table for the ID and Name column separated by the “|” character.

Our column names are “ID” and “Name” for the Test table. Let’s start renaming the column names using the RENAME COLUMN instruction. We will be renaming the column “Name” to “Fname” using the ALTER TABLE instruction followed by the table name “Test” and the “RENAME COLUMN” using the “TO” keyword. The query was successful, as shown below:

sqlite> ALTER TABLE Test RENAME COLUMN Name TO Fname;

After altering the column’s name for the table “Test”, we will use the SELECT instruction to display all the table’s records. A total of five records have been displayed, as presented below:

sqlite> SELECT * FROM Test;

Let’s see how updating a new name works or not. Let’s insert the records within the Test table using the same llamativo names of columns for the table Test. Thus, we have tried the INSERT INTO instruction with the llamativo names of the “ID” and “Name” column followed by the VALUES keyword and the 6th record, i.e., (6, “Barak”). Execution of this instruction returns an error “table test has no column named “Name”. This error has occurred due to the usage of the llamativo column’s name “Name” instead of the new column name “Fname”.

Let’s insert the same record with the new column name “Fname” instead of the llamativo column name “Name” via the INSERT INTO instruction usage in the terminal. This time, we didn’t have any errors after executing this insertion command. We have displayed all the records of the Test table using the SELECT instruction followed by the asterisk “*” character. A total of six records have been displayed, i.e., the last record is the newest inserted record with the new column name “Fname”.

Just like the INSERT instruction, we can also use the SELECT instruction to fetch the records of a table and use the column name within it to display that the new name has been successfully added to the table column. So, we have been utilizing the SELECT instruction to display the Test table records while adding a WHERE clause condition specified. For this, we have been using the llamativo column’s name, “Name”, to display only the records from the table where the value in the Name column is “Ana”. Execution of this query displayed an error, “no such column: Name”. The reason for this error is the newly updated column’s name to “Fname”. Let’s run the same query with the new column name “Fname” to fetch all the records where the “Fname” column contains the value “Ana”. It displayed a single record from the table and removed the error.

sqlite> SELECT * FROM Actor WHERE Name = «Ana»;

sqlite> SELECT * FROM Actor WHERE FName = «Ana»;

Conclusion

This article discussed using the RENAME COLUMN clause within the ALTER TABLE instruction to update or modify the name of a specific column from the table. The example can be amended as well. We have done it so far in the simplest way possible and hope you like it.



Source link


“In our life, we can rename a file by using manual methods. But here in C# language, we have different methods to rename our existing file. The C# programming allows us to rename a file using different built-in methods. We can easily rename our existing file by writing a simple code. In this tutorial, we will explain to you in a very easy and understandable way how you can rename your existing file by writing a few lines of code in C# programming. We demonstrate different examples of using different methods to rename our file.”

Methods for Renaming a File in C#

Example # 1: Using Move() Method

In this given example, we are going to use the Move() method for renaming a file in C# programming. We perform these examples in Ubuntu 20.04 text editor. The file extension for this C# code is “.cs.” So, you must save your file with this file extension.

We start our C# code with “using System.” We use a library in our program to access classes such as the “Console” class and functions such as the “WriteLine” function. We also import “System.IO,” used for input/output. If we don’t use this “System.IO,” the program doesn’t get the file from your system. It gives an error. The namespace is here with the name “Demo.” We also declare a class with the name “Program” and call our “main” function. Our C# program starts from this “main” function when executing our code.

Here, we just give the file name because this C# code and the file we want to rename are in the same directory. If you want to rename a file in the other directory, you must give the proper path of the directory. After this, we declare another variable of a string data type with the name “location2” and give the file’s new name. Here we want to change the name of file “MyProgram.txt” file to “MyFirstProgram.txt,” so we store this name in our string variable “location2”.

Now, we use File.Move() method for renaming the existing file. In “File.Move(location1, location2)”, we give our string variables so that it renames our location1 file with the location2 file name. When it moves this file and renames it, then we have to delete this “location1” file, so for this, we use “File.Delete(location1)”. It will delete our previous file and save it with the new name, which means it renames our existing file. After renaming the file, we want to print a line on the screen, so for this, we use the “Console.WriteLine()” method and then close all the brackets and save this code.

We can get the output of this code by using the “mcs” command for compiling our code and the “chimpancé” command for executing this compiled code in Ubuntu 20.04. When this code is compiled and executed successfully, we get the output, and our existing file is renamed.

In this output, you can see that both commands run successfully, and the existing file is renamed, and then it prints the line on the terminal that is “The file successfully renamed.”

Example # 2: Using the Copy() Method

We give another example of using the Copy() method for renaming our existing file in C# programming. Check this example of how this method renames a file.

In this example, the “namespace” is named “Csharp,” and the class name here is “Example.” After invoking the “main” function, we are going to declare two variables of the “string” data type with the name “loc1” and “loc2,” respectively. We store the path of the existing file which we want to rename in “loc1,” which is “MyFirstProgram.txt,” and we want to change this file name and give a new name to this file which is “MyCsharpProgram.cs”, so we store this new file name and path to the second “string” variable “loc2”. We must put the “@” sign whenever we give any file path.

Here, we give the filename because this code file and the file we want to rename are saved in the same directory as we explained in the previous example. After this, we use the Copy() method. The copy() method is used for renaming a file. It will copy the existing file to a new file and change the new file’s name. Here, we have “File.Copy(loc1, loc2, true),” which means if we have this file whose path is given in “loc1,” then this file is copied and saved with the new name which we give in the “loc2” string variable.

After copying this, we deleted our previous file, which is stored in “loc1” by using the “File.Delete(loc1)” method. After successfully renaming the file, we have to print one line on the screen, which is “The file Renamed using Copy ()” with the help of the “Console.WriteLine()” function. The output of this code is given in the image below:

Example # 3

In this example, we give the file name that doesn’t exist in the given directory and use the try-catch method, which displays an exception. Let’s try this example of how it shows an exception when the file is not found.

Here we declare a public class “Csharp” and have a “main” function inside this “Csharp class.” We have two string variables named “source” and “destination.” We initialize the “source” string variable with the file name, which doesn’t exist in the directory. And initialize the second variable, “destination,” with the new file name. After this, we utilize the try-catch method here. The “try” method works when the file exists in the directory; otherwise, it moves to the catch statement, which throws the exception.

In this try method, we have the File.Move() method, which we discussed in our first example. This method works if the file exists. If the file exists, then it moves the file and renames it. We have an “if” statement that checks if the file saved in the “source” variable does not exist, which means; the file is renamed. In the catch statement, we have “IOException,” which throws an exception while accessing the file. And stores this exception in “e.” We will print the exception also by using “ConsoleWriteLine.”

In the output, we see that it throws an exception that “file not found” because the path or name of the file is not correct, so it throws an exception. We must give the correct name and path for renaming that file; otherwise, it throws an exception.

Example # 4

We use the same example, i.e., example 3, and give the correct file name for renaming it. In this example, we give the name of the file that exists in the directory, and we rename the file with the help of the same code we discussed in example 3.

This code is the same as discussed in our previous example 3. But here, we store the correct file in the “source” variable and rename this file by using the same code. We discuss this code in example 3 in detail.

Just look at the output. If we give the correct path and correct file name for renaming the file, then our file is renamed successfully, and it doesn’t throw an exception.

Conclusion

In this tutorial, we have explained how to rename a file in C# programming. We have discussed this renaming file concept in detail and show you how to give the path and the file name for renaming the existing file. In this tutorial, we have used two different methods for renaming the existing file. We also show you what happens if the file does not exist. And how it throws exceptions when the file does not exist in the directory. I hope you will understand this tutorial, and it will be helpful for you when you want to rename your existing file.



Source link