I am currently working on a project that creates/drops tables to MySql and also creates the "Posts" that go into each. When I try and run what I have it somehow takes my database name and tries to use it as a table. Any and all help is appreciated! I'm really confused how that value is moving from ConnectionString to SavePost
public class SavePost : ISavePost
{
public static void CreatePostTable()
{
ConnectionString myConnection = new ConnectionString();
string cs = myConnection.cs;
//Console.WriteLine(cs);
using var con = new MySqlConnection(cs);
con.Open();
string stm = @"CREATE TABLE post(Id INTEGER PRIMARY KEY AUTO_INCREMENT, Text TEXT, Timestamp DATE)";
using var cmd = new MySqlCommand(stm, con);
cmd.ExecuteNonQuery();
}
public void CreatePost(Post myPost)
{
ConnectionString myConnection = new ConnectionString();
string cs = myConnection.cs;
using var con = new MySqlConnection(cs);
con.Open();
string stm = @"INSERT INTO post(Id, Text, Timestamp) VALUES(@id, @text, @Timestamp)";
using var cmd = new MySqlCommand(stm, con);
cmd.Parameters.AddWithValue("@Id", myPost.Id);
cmd.Parameters.AddWithValue("@Text", myPost.Text);
cmd.Parameters.AddWithValue("@TimeStamp", myPost.Timestamp);
cmd.Prepare();
cmd.ExecuteNonQuery();
}
void ISavePost(Post myPost)
{
throw new System.NotImplementedException();
}
void ISavePost.SavePost(Post myPost)
{
throw new NotImplementedException();
}
}
Connection String (Only Place my data base name exists)
public class ConnectionString
{
public string cs {get; set;}
public ConnectionString()
{
string server = "ohunm00fjsjs1uzy.cbetxkdyhwsb.us-east-1.rds.amazonaws.com";
string database = "z7h5wlemxfp9e62l"; //Database Name Here
string port = "3306";
string userName = "b0carrau6sztptpq";
string password = "glfh8lqesdwi10tb";
cs = $@"server = {server};user = {userName};database = {database};port = {port};password = {password};";
}
}
Main
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
//DeletePost.DropPostTable();
//SaveBook.CreatePostTable();
Post myPost = new Post(){Id = 1, Text = "Roll Tide"};
myPost.Save.CreatePost(myPost);
}
Post(Not sure if needed, but thought it wouldn't hurt)
public class Post
{
public int Id{get; set;}
public string Text{get; set;}
public DateTime Timestamp{get; set;}
public ISavePost Save{get; set;}
public Post()
{
Save = new SavePost();
}
ERROR FEEDBACK
Unhandled exception. MySql.Data.MySqlClient.MySqlException (0x80004005): Table 'z7h5wlemxfp9e62l.post' doesn't exist
at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId)
at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
at mis331_pa3_abrush60.database.SavePost.CreatePost(Post myPost) in /Users/treyrush/Desktop/PA3/mis331-pa3-abrush60/database/SavePost.cs:line 45
at mis331_pa3_abrush60.Program.Main(String[] args) in /Users/treyrush/Desktop/PA3/mis331-pa3-abrush60/Program.cs:line 17
CodePudding user response:
The fully qualified name of the table includes the database (schema) that it is in. So, if your database is named bobby
and your table is named posts
, you access that table using bobby.posts
Now, when connecting it is very common to specify the default schema, which you do. That saves the need to prepend the schema to the table name each time you name a table. However, internally, it is still used and for error messages it is very helpful to see the schema.
So when it says Table 'z7h5wlemxfp9e62l.post' doesn't exist
that simply means the table post
does not exist in database z7h5wlemxfp9e62l