Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Gilead7 · Jan 07, 2016 at 09:12 PM · c#prefablistsqlitedatetime

Database input datetime

I'm building an app and ran into an issue. In all the other tables I've used strings. In this one, I'm using datetime for id. string GigID=System.DateTime.Now.ToString("MM/dd/yyyy"); I'm using the SQLIter asset pack from the assetstore. Thanks in advance!

While it seemed to work, I cannot enter anything into the SQLite database(was able to get one in, not sure how) and when I try to display them, I get an error Format Exception on the DATETIME. Table was set up thusly mSQLString = "CREATE TABLE IF NOT EXISTS " + SQL_TABLE_NAME + " (" + COL_TN_ID + " DATE PRIMARY KEY, " + COL_TN_NAME + " TEXT," + COL_TN_ADDRESS + " TEXT," + COL_TN_DATE + " TEXT," + COL_TN_TIME + " TEXT)";

Form: public void Form() { string ID=System.DateTime.Now.ToString("MM/dd/yyyy"); GUI.Label (new Rect (Screen.width/2, 10, 700, 50), "Add-a-Tron!"); GUI.Label (new Rect (10, 30, 700, 50), "Enter the name of the event" ); Name=GUI.TextField(new Rect(155, 50, 100, 25), Name); GUI.Label (new Rect (10, 70, 700, 50), "Enter the Address."); Address=GUI.TextField(new Rect(155, 90, 100, 25), Address); GUI.Label (new Rect (10, 110, 700, 50), "Enter the date of the event" ); Date=GUI.TextField(new Rect(155, 120, 100, 25), Date); GUI.Label (new Rect (10, 130, 700, 50), "Enter the time of the event" ); Time=GUI.TextField(new Rect(155, 140, 110, 25), Time); if(GUILayout.Button( "Add Gig")) { Add(ID, Name, Address, Date, Time);

             }
         }

Add to the Database: (All other functions have worked to put stuff in the database) public void Add(string ID, string Name, string Address, string Date, string Time) { Debug.Log(ID + " "+ Name + " " + Address + " " + Date + " " +Time);

             mSQLString = "INSERT OR REPLACE INTO " + SQL_TABLE_NAME
                 + " ("
                     + COL_TN_ID + ","
                     + COL_TN_NAME + ","
                     + COL_TN_ADDRESS + ","
                     + COL_TN_DATE + ","
                     + COL_TN_TIME
                     + ") VALUES ("
                     + "'" + ID + "',"  // note that string values need quote or double-quote delimiters
                     + "'" + Name + "',"
                     + "'" + Address + "',"
                     + "'" + Date + "',"
                     + "'" + Time +"');";
             
             Debug.Log(mSQLString + "Added!");
             ExecuteNonQuery(mSQLString);
         }

To display them onscreen, I made a prefab and hooked it up. Here are the functions that handle that. I did put in a test entry using the newest DB Browser for SQLite:

 public void GetAll()
     {
         Listname.Clear();
         mConnection.Open();
         mSQLString = "SELECT * FROM " + SQL_TABLE_NAME; 
         mCommand.CommandText = mSQLString;
         mCommand.ExecuteNonQuery();
         mReader = mCommand.ExecuteReader();
         
         while (mReader.Read())
         {
             listname.Add(new listname(mReader.GetString(0), 
                               mReader.GetString(1), 
                               mReader.GetString(2), 
                               mReader.GetString(3), 
                               mReader.GetString(4)));
 
             //Debug.Log(mReader.GetDateTime(0) + mReader.GetString(1) +mReader.GetString(2) + mReader.GetString(3) + mReader.GetDateTime(4) );
         }

Finally...

 public void ShowAll()
     {    Debug.Log(listname.Count);
         for(int i = 0; i< listname.Count; i++)
         {
             GameObject giObj = Instantiate(thePrefab);
             listname tmpVar = listname[i];
             giObj.GetComponent<theScript>().Displaylistnames(tmplistname.ID, tmplistname.Name, tmplistname.Address, tmplistname.Date, tmplistname.TIme);
             giObj.transform.SetParent(Equipparent); 
             
         }
 
     }




Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by phil_me_up · Jan 07, 2016 at 09:52 PM

I've not read all your code, but I expect the problem is that you're trying to use a DateTime as a primary key. In SQL terms, the primary key must be unique and a datetime (especially one restricted to just the year, month and day) will only be unique if you only expect one entry per day. I guess this is how you managed to enter one row.

You might be able to get away with including hours, minutes and seconds to form the id but it's probably not a good idea as two rows within the same second will cause issues. Also, you generally shouldn't be using a string for your ID as it won't be efficient when querying the DB.

So, in short, make sure your Primary key is an ID or something that is guaranteed to be unique. Either a simple integer or if supported, a GUID. This can be auto generated for you by your database when setup properly and inserting a new row.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Gilead7 · Jan 09, 2016 at 05:48 PM

I suddenly realized after posting this that I had moved the project to another drive and had been looking at the database from the original! D'OH! Things had been saved after all! I think I will switch over to an integer id instead. Should I use AUTOINCREMENT after primary key specificity? There had been some controversy about using it.

If anyone else has ideas or suggestions, please let me know. Thanks!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image phil_me_up · Jan 09, 2016 at 06:07 PM 0
Share

The autoincrement issue is a divisive one.

If you can use another, useful ID that you know to be unique (like a user-id from some other service), then it's ok and there is no need to use AUTOINCRE$$anonymous$$ENT. However, if you don't have such a key, or if there is any chance you'll be expecting duplicate entries then just use an autoincremented id (or an auto-created GUID if available).

Remember that it's often advantageous to mark other columns as indexes to help with queries etc too (i.e if you have row_id as a primary id, a user_id could be an index if you expect to see this user id more than once in the table). This will help you do queries like 'select x, y, z where user_id = 123'

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

53 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I Instantiate a prefab in a list? 0 Answers

A node in a childnode? 1 Answer

Multiple Cars not working 1 Answer

Populating a list with different items in different clones of the same prefab. 0 Answers

Return List from Singleton 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges