How to Add Items to Database

How to add items to database?

How about?

private void button1_Click(object sender, EventArgs e)
{
sqlCEC.Open();
SqlCeCommand sqlCEcommand = new SqlCeCommand("INSERT INTO [Project Table]([Project Name]) VALUES([@Project Name])", sqlCEC);
sqlCEcommand.Parameters.AddWithValue("@Project Name", new_project_name.Text);
sqlCEcommand.ExecuteNonQuery();
sqlCEC.Close();
}

Because your database objects contains spaces, you need to enclose them with []

My humble advice is, never use spaces or other special characters in your tables or your column names.

Add Items in Sales Transaction table as single transaction

To add a column, you need to alter the current table:

ALTER TABLE "Sales Transaction"
ADD COLUMN TransactionNumber INT NOT NULL

When you are inserting into this table you will need something like this:

DECLARE @TransNumber INT = (SELECT CASE WHEN MAX(TransactionNumber) IS NULL THEN 1 ELSE MAX(TransactionNumber) + 1 END FROM "Sales Transaction")

INSERT INTO "Sales Transaction" ("Item Name", Qty., Price, "Cust. Name", Tax, Discount, Total, TransactionNumber)
VALUES
('Pen',1,10,'John Smith',0,0,10,@TransNumber),
('Pencil',2,7,'John Smith',0,0,14.00,@TransNumber)

Since the TransactionNumber will be the same for both rows, you can search by that.

How to add an item to database directly in django without forms

This is because you insert it twice. The .create(…) method [Django-doc] already inserts in in the database. So you can implement this as:

def display(request):
print('display functio')
d=upload.objects.last()
test=sr.takeCommand(d.file.path)
# will store the record in the database
p = text.objects.create(texts=test)
print(test)
return render(request,'thanks.html',{'print':test})

you can link it to d with:

def display(request):
print('display functio')
d=upload.objects.last()
test=sr.takeCommand(d.file.path)
# will store the record in the database
p = text.objects.create(texts=test, upload_text=d)
print(test)
return render(request,'thanks.html',{'print':test})


Note: Models in Django are written in PerlCase, not
snake_case, so you might want to rename the model from text to Text.

Insert data into Database - What is the best way to do it

Insert data into Database - What is the best way to do it

I suggest you to create own object that will represent your table where properties of object will be equal to columns in table, e.q.

public class Contact {

private String name;
private String number;
private String image;
private boolean conn;

//getters and setters
}

Now your approach sounds like "spaghetti code". There is not need to have four ArrayLists and this design is not efficient and correct.

Now, you will have one ArrayList of Contact object with 500 childs.

What is the best way to insert?

For sure wrap your insertion to one TRANSACTION that speed up your inserts rapidly and what is the main your dealing with database becomes much more safer and then you don't need to care about possibility of losing database integrity.

Example of transaction(one method from my personal example project):

public boolean insertTransaction(int count) throws SQLException {
boolean result = false;
try {
db = openWrite(DataSource.getInstance(mContext));
ContentValues values = new ContentValues();
if (db != null) {
db.beginTransaction();
for (int i = 0; i < count; i++) {
values.put(SQLConstants.KEY_TYPE, "type" + i);
values.put(SQLConstants.KEY_DATE, new Date().toString());
db.insertOrThrow(SQLConstants.TEST_TABLE_NAME, SQLConstants.KEY_TYPE, values);
values.clear();
}
db.setTransactionSuccessful();
result = true;
}
return result;
}
finally {
if (db != null) {
db.endTransaction();
}
close(db);
}
}

How to insert multiple items into database when using Scrapy?

You need to refactor your pipeline into something like this:

class DatabasePipeline(object):

def open_spider(self, spider):
#Create database connection
...
#create items list
self.items = []

def process_item(self,item,spider):
self.items.append(item)
if len(self.items)==100:
#constuct SQL query to insert multiple records
...
#execute query and clean self.items
self.items = []
return item

def close_spider(self,spider):
#insert remaining records
if self.items:
#constuct SQL query to insert multiple records
...
#execute query
#close database connection

Add objects from database to a List, using LINQ to SQL

Just return the List of book as below:

public List<book> addBook()
{
return (from book in db.books
where book.bookID == 1
select book)
.ToList();
}

And it's weird to name the method addBook as the method's outcome is to return a list of book.

Name it as: public List<book> GetBooks()

While List<T>.Add(T) Method only allow to add single item to list, to add multiple items into list, you need List<T>.AddRange(IEnumerable<T>) Method.



Related Topics



Leave a reply



Submit