我正在尝试使用以下代码在SQLite数据库上插入值:
var ImageIndex = 0;
function swipe(event){
var touch = event.targetTouches[0];
var midpoint = Math.floor(screen.width/2);
var px = touch.pageX;
var items = document.getElementsByClassName('image-wrapper');
var itemActive = items[ImageIndex];
if(px < midpoint){
itemActive.style.marginLeft = '-100%';
itemActive.style.transition = '1s';
if (ImageIndex < (items.length - 1)) {
ImageIndex = ImageIndex + 1;
}else{
ImageIndex = 0;
}
}else{
itemActive.style.marginLeft = '0px';
itemActive.style.transition = '1s';
if (ImageIndex >= 1 ) {
ImageIndex = ImageIndex - 1;
}else{
ImageIndex = items.length - 1;
}
}
}
这是xaml代码:
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
string database_connection = "Data Source=database.db;Version=3;";
SQLiteConnection connection = new SQLiteConnection(database_connection);
connection.Open();
string query = String.Format("INSERT INTO `database`.`airports`(`id`, `airportName`, `airportCity`, `active`) VALUES ('"+txtAirportId+"','"+txtAirportName+"','"+txtAirportCity+"','"+1+"')");
SQLiteCommand command = new SQLiteCommand(query, connection);
SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(command);
connection.Close();
txtAirportId.Clear();
txtAirportName.Clear();
txtAirportCity.Clear();
}
因此,当我单击<DataGrid x:Name="dgAirports" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="100" Width="272" ItemsSource="{Binding}" AutoGenerateColumns="False" CanUserAddRows="false" VerticalScrollBarVisibility="Hidden">
<DataGrid.Columns>
<DataGridTextColumn Header="Шифра" Binding="{Binding Path = id}" Width="*"/>
<DataGridTextColumn Header="Назив" Binding="{Binding Path = airportName}" Width="*"/>
<DataGridTextColumn Header="Град" Binding="{Binding Path = airportCity}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
<Label x:Name="lblAirportId" Content="ИД" HorizontalAlignment="Left" Margin="123,115,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtAirportId" Text="{Binding ElementName=dgAirports, Path=SelectedValue.id}" HorizontalAlignment="Left" Height="23" Margin="157,115,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="125"/>
<Label x:Name="lblAirportName" Content="Назив" HorizontalAlignment="Left" Margin="109,142,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtAirportName" Text="{Binding ElementName=dgAirports, Path=SelectedValue.airportName}" HorizontalAlignment="Left" Height="23" Margin="157,143,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="125"/>
<Label x:Name="lblAirportCity" Content="Град" HorizontalAlignment="Left" Margin="115,168,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtAirportCity" Text="{Binding ElementName=dgAirports, Path=SelectedValue.airportCity}" HorizontalAlignment="Left" Height="23" Margin="157,171,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="125"/>
<Button x:Name="btnShow" Content="Прикажи" HorizontalAlignment="Left" Margin="10,115,0,0" VerticalAlignment="Top" Width="75" Click="btnShow_Click"/>
<Button x:Name="btnAdd" Content="Додај" HorizontalAlignment="Left" Margin="10,142,0,0" VerticalAlignment="Top" Width="75" Click="btnAdd_Click"/>
</Grid>
时,什么也没有发生,但没有任何错误。那么,我所缺少的以及如何将这些值插入“机场”表中?
答案 0 :(得分:-1)
只需像这样更改您的功能:
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
string database_connection = "Data Source=database.db;Version=3;";
SQLiteConnection connection = new SQLiteConnection(database_connection);
connection.Open();
string query = String.Format($"INSERT INTO `database`.`airports`(`id`, `airportName`, `airportCity`, `active`) VALUES ('{txtAirportId }','{txtAirportName }','{txtAirportCity}','1')");
SQLiteCommand command = new SQLiteCommand(query, connection);
command.ExecuteNonQuery();
txtAirportId.Clear();
txtAirportName.Clear();
txtAirportCity.Clear();
}
顺便说一句:您的代码仍然不好。如果仅联系字符串,至少您将具有SQL Injection漏洞。使用参数或阅读有关Entity Framework Core技术的信息。