insert-update

How to get the original value of changed fields?

帅比萌擦擦* 提交于 2019-11-29 04:06:27
I'm using sqlalchemy as my orm, and use declarative as Base. Base = declarative_base() class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) name = Column(String) My question is, how do I know a user has been modified, and how to get the original values without query database again? user = Session.query(User).filter_by(id=user_id).first() # some operations on user .... # how do I know if the user.name has been changed or not? ... # How do get the original name? Thanks in advance! UPDATE I found a method get_history can get the history value of a field, but I'm not

How to INSERT a record or UPDATE if it already exists?

◇◆丶佛笑我妖孽 提交于 2019-11-28 22:35:23
I have a table with columns record_id (auto inc), sender , sent_time and status . In case there isn't any record of a particular sender, for example "sender1", I have to INSERT a new record otherwise I have to UPDATE the existing record which belongs to "user1". So if there isn't any record already stored, I would execute # record_id is AUTO_INCREMENT field INSERT INTO messages (sender, sent_time, status) VALUES (@sender, time, @status) Otherwise I would execute UPDATE statement. Anyway.. does anyone know how to combine these two statements in order to insert a new record if there isn't any

MySQL ON DUPLICATE KEY UPDATE while inserting a result set from a query

佐手、 提交于 2019-11-28 13:23:01
I am querying from tableONE and trying to insert the result set into tableTWO. This can cause a duplicate key error in tableTWO at times. So i want to ON DUPLICATE KEY UPDATE with the NEW determined value from the tableONE result set instead of ignoring it with ON DUPLICATE KEY UPDATE columnA = columnA . INSERT INTO `simple_crimecount` (`date` , `city` , `crimecount`)( SELECT `date`, `city`, count(`crime_id`) AS `determined_crimecount` FROM `big_log_of_crimes` GROUP BY `date`, `city` ) ON DUPLICATE KEY UPDATE `crimecount` = `determined_crimecount`; # instead of [ON DUPLICATE KEY UPDATE

Update the value of a field in database by 1 using codeigniter

有些话、适合烂在心里 提交于 2019-11-28 12:00:45
I want to implement a SQL statement using codeigniter active record. UPDATE tags SET usage = usage+1 WHERE tag="java"; How can I implement this using Codeigniter active records? Regards $this->db->set('usage', 'usage+1', FALSE); $this->db->where('tag', 'java'); $this->db->update('tags'); Srihari Goud You can also use something like this $data = array('usage' => 'usage+1', *other columns*); $this->db->where('tag', 'java'); $this->db->update('tags', $data); UPDATE: $data was not being passed on to update I find its sometimes simpler to just write the SQL rather than having Active Record build it

How to insert JPEG into a SQL Server 2000 database field of image type using Transact SQL

∥☆過路亽.° 提交于 2019-11-28 07:30:55
问题 I'm trying to figure out how to insert a .JPG file into a SQL Server 2000 database field of type image using Transact SQL. Thanks. 回答1: Use OPENROWSET: INSERT MyTable (ImageColumnName) SELECT BulkColumn FROM OPENROWSET (BULK 'c:\myjpeg.jpg', SINGLE_BLOB) AS X EDITED Whoops, you're using 2000--the previous solution is not supported. You have to use WRITETEXT: CREATE TABLE MyTable ( ID INT PRIMARY KEY IDENTITY (1,1), ImageColumnName IMAGE NULL ) GO -- must insert a dummy value into the image

.NET Dictionary: get or create new

。_饼干妹妹 提交于 2019-11-28 07:12:43
I often find myself creating a Dictionary with a non-trivial value class (e.g. List ), and then always writing the same code pattern when filling in data. For example: var dict = new Dictionary<string, List<string>>(); string key = "foo"; string aValueForKey = "bar"; That is, I want to insert "bar" into the list that corresponds to key "foo" , where key "foo" might not be mapped to anything. This is where I use the ever-repeating pattern: List<string> keyValues; if (!dict.TryGetValue(key, out keyValues)) dict.Add(key, keyValues = new List<string>()); keyValues.Add(aValueForKey); Is there a

Django - save() update on duplicate key

﹥>﹥吖頭↗ 提交于 2019-11-28 06:53:17
I have little application which allows a user to rate a video. The user can rate only once. So I have defined the uniqueness on the model. But he should be able change his rate. So the save() should update on duplicate key class VideoRate(models.Model): """Users can Rate each Video on the criterias defined for the topic""" user = models.ForeignKey(User) video = models.ForeignKey(VideoFile) crit = models.ForeignKey(VideoCrit) rate = models.DecimalField(max_digits=2, decimal_places=1, choices=RATE_CHOICES) class Meta: unique_together = (('user', 'video', 'crit'),) verbose_name = 'Video Rating'

Why are 2 rows affected in my `INSERT … ON DUPLICATE KEY UPDATE`?

本小妞迷上赌 提交于 2019-11-28 04:02:27
I'm doing an INSERT ... ON DUPLICATE KEY UPDATE for a PRIMARY KEY in the following table: DESCRIBE users_interests; +------------+---------------------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+---------------------------------+------+-----+---------+-------+ | uid | int(11) | NO | PRI | NULL | | | iid | int(11) | NO | PRI | NULL | | | preference | enum('like','dislike','ignore') | YES | | NULL | | +------------+---------------------------------+------+-----+---------+-------+ However, even though these values should be unique, I

I can not update a record in Parse; Error: “object not found for update (Code: 101, Version: 1.2.16)”

血红的双手。 提交于 2019-11-28 02:50:03
问题 I am using Parse as my backend for one of my native iOS app. I am not able to update a record on a table in Parse. First I query the object that I want to update it. So, "objects" array returns 1 record but when I want to update it I get the following error; "object not found for update (Code: 101, Version: 1.2.16)" So what do you think I can not update a record in Parse? (Below is my code) Thanks, E. PFQuery *query = [PFQuery queryWithClassName:@"askedFriends"]; [query whereKey:@"questionID"

Update record with previous row

谁都会走 提交于 2019-11-28 02:23:28
I have a situation where I need to update the records with previous row value. Source: |MatId | BaseId |Flag|Pkg1| CS1 -------------------------------- |3001 | 3001 | 1 | 20 | 2 | |3002 | 3001 | 0 | 15 | 3 | |3003 | 3001 | 0 | 10 | 4 | Here both 3001 ( MatID ) and 3001( BaseID ) are same so FLAG =1, in the next record only BASEID is same. The output should be only PKG1 field updated with the current row value. Target or output: |MatId | BaseId|Flag|Pkg1|CS1 ------------------------------ |3001 | 3001 | 1 | 20 | 2| |3002 | 3001 | 0 | 20 | 3| |3003 | 3001 | 0 | 20 | 4| As seen in the target