继续,插入BookInfo的信息;

图片 1

DB::table($tablename)获取科学带前缀的表名,转变数据库句柄,

四、修改ProviderTest主代码:

查询DB,看看有比少之甚少进DB:

 

举例:

  3卡塔尔update()方法:先拿走SQLiteDatabase
的实例,然后依据传入的 Uri 参数判定出客商想要更新哪张表里的多寡,再调用
SQLiteDatabase的
update()方法进行更新,最终将受影响的行数作为重返值再次来到。

 1 CREATE PROCEDURE [base].[Publisher#Insert](@json nvarchar(max), @id int out)
 2 WITH ENCRYPTION
 3 AS
 4 BEGIN
 5 ...
 6 
 7         declare    @name nvarchar(100);
 8         select    @name=Publisher from openjson(@json, '$') with (Publisher nvarchar(100))
 9 
10         -- insert Publisher
11         insert    base._Publisher(Name)select @name
12         where    not exists(select 1 from base._Publisher p where p.Name=@name);
13 
14         select    @id=ID from base.Publisher#Raw() where Name=@name;
15 ...
16 END
1 CREATE SCHEMA [svc]
2     AUTHORIZATION [dbo];

表内有以下字段:id(递增卡塔尔国 name uid 字段类型就别管了

 

 1 CREATE PROCEDURE [base].[Binding#Insert](@json nvarchar(max), @id int out)
 2 WITH ENCRYPTION
 3 AS
 4 BEGIN
 5 ...
 6 
 7         declare    @name nvarchar(100);
 8         select    @name=Binding from openjson(@json, '$') with (Binding nvarchar(100))
 9 
10         -- insert Binding
11         insert    base._Binding(Name)select @name
12         where    not exists(select 1 from base._Binding p where p.Name=@name);
13 
14         select    @id=ID from base.Binding#Raw() where Name=@name;
15 
16 ...
17 END

前段时间的书屋起始化的前端消息已经圆满,所今后后开端兑现DB的Script部分。

获得单条数据:

图片 2图片 3

昨夜完结了Web端新添图书音讯的法力,今后就差DB的切切实实落到实处了。

 点击Save开关提交,OK,不奇怪提交了并跳转了。

DB::田野(field)(字段名, $pid) 再次回到条件,假如为数组则赶回 in 条件

  当按下Delete后,再按下询问,当时已不多展现了。

 

新增Action:Shelf_Init.sql

DB::fetch_all(‘SELECT * FROM %t WHERE panel=%d’,
array($this->_table, $panel), ‘uid’);

  4卡塔尔国delete()方法:仍然为先拿走到
SQLiteDatabase 的实例,然后依据传入的 Uri
参数判定出顾客想要删除哪张表里的数据,再调用 SQLiteDatabase 的
delete()方法举办删除就好了,被去除的行数将用作重返值重返。

 1 CREATE PROCEDURE [base].[BookTranslator#Insert](@json nvarchar(max), @bookID bigint)
 2 WITH ENCRYPTION
 3 AS
 4 BEGIN
 5 ...
 6 
 7         -- insert Translator
 8         insert    base._Author(Name)select value
 9         from    openjson(@json, '$.Translators') x
10         where    not exists(select 1 from base._Author p where p.Name=x.value);
11 
12         insert    base._BookTranslator(BookID, TranslatorID) select @bookID, x.ID
13         from    openjson(@json, '$.Translators') j join base.Author#Raw() x on x.Name=j.value
14 
15 ...
16 END

Init.cshtml

DB::limit(n,n)再次回到约束字串

 

继续,插入BookTag信息;

 1 <form method="post">
 2     <div class="form-group form-group-lg">
 3         <label asp-for="Input.NickName"></label>
 4         <input class="form-control form-control-lg" asp-for="Input.NickName" autocomplete="off">
 5         
 6     </div>
 7     <div class="form-group form-group-lg">
 8         <label asp-for="Input.ShelfName"></label>
 9         <input class="form-control form-control-lg" asp-for="Input.ShelfName" autocomplete="off">
10         
11     </div>
12     <div class="form-group text-right">
13         <button class="btn btn-warning btn-lg" type="submit">Save</button>
14     </div>
15 </form>

DB::fetch_all($sql)查询并fetch

 

接下来,将图纸路线保存到FileBank中,并回到FileBankID;

….

获取多条数据:

三、编写ProviderTest的布局文件:

 

 

调用形式:DB::insert()

View Code

好了,开头测验。

 1 CREATE PROCEDURE [svc].[Shelf$Init](@json nvarchar(max))
 2 WITH ENCRYPTION
 3 AS
 4 BEGIN
 5     SET    NOCOUNT    ON;
 6     SET XACT_ABORT ON;
 7     BEGIN TRY
 8         BEGIN    TRAN;
 9 
10         declare    @nickName nvarchar(20), @shelfName nvarchar(20);
11         select    @nickName=NickName,     @shelfName=ShelfName
12         from    openjson (@json, '$')
13         with (
14             NickName        nvarchar(20),
15             ShelfName        nvarchar(20)
16         );
17 
18         insert    core._Party(Type, Alias) select k._User, @nickName
19         from    core.Party#Type() k;
20         declare    @userID int=@@identity;
21 
22         
23         insert    core._Party(PID, Type, Alias) select @userID, k._Shelf, @shelfName
24         from    core.Party#Type() k;
25 
26         COMMIT    TRAN;
27     END TRY
28     BEGIN CATCH
29         if (xact_state() = -1) ROLLBACK TRAN; throw;
30     END CATCH
31 END

DB::delete($tablename, 条件,条数约束)删除表中的数量

 1 public class MainActivity extends AppCompatActivity {
 2 
 3     private String newId;
 4     @Override
 5     protected void onCreate(Bundle savedInstanceState) {
 6         super.onCreate(savedInstanceState);
 7         setContentView(R.layout.activity_main);
 8         Button addData = (Button) findViewById(R.id.add_data);
 9         addData.setOnClickListener(new View.OnClickListener() {
10             @Override
11             public void onClick(View v) {
12             // 添加数据
13                 Uri uri = Uri.parse("content://com.mycompany.database.provider/book");
14                 ContentValues values = new ContentValues();
15                 values.put("name", "A Clash of Kings");
16                 values.put("author", "George Martin");
17                 values.put("pages", 1040);
18                 values.put("price", 22.85);
19                 Uri newUri = getContentResolver().insert(uri, values);
20                 newId = newUri.getPathSegments().get(1);
21             }
22         });
23         Button queryData = (Button) findViewById(R.id.query_data);
24         queryData.setOnClickListener(new View.OnClickListener() {
25             @Override
26             public void onClick(View v) {
27                 // 查询数据
28                 Uri uri = Uri.parse("content://com.mycompany.database.provider/book");
29                         Cursor cursor = getContentResolver().query(uri, null, null, null, null);
30                 if (cursor != null) {
31                     while (cursor.moveToNext()) {
32                         String name = cursor.getString(cursor.
33                                 getColumnIndex("name"));
34                         String author = cursor.getString(cursor.
35                                 getColumnIndex("author"));
36                         int pages = cursor.getInt(cursor.getColumnIndex
37                                 ("pages"));
38                         double price = cursor.getDouble(cursor.
39                                 getColumnIndex("price"));
40                         Log.d("MainActivity", "book name is " + name);
41                         Log.d("MainActivity", "book author is " + author);
42                         Log.d("MainActivity", "book pages is " + pages);
43                         Log.d("MainActivity", "book price is " + price);
44                     }
45                     cursor.close();
46                 }
47             }
48         });
49         Button updateData = (Button) findViewById(R.id.update_data);
50         updateData.setOnClickListener(new View.OnClickListener() {
51             @Override
52             public void onClick(View v) {
53             // 更新数据
54                 Uri uri = Uri.parse("content://com.mycompany.database.provider/book/" + newId);
55                         ContentValues values = new ContentValues();
56                 values.put("name", "A Storm of Swords");
57                 values.put("pages", 1216);
58                 values.put("price", 24.05);
59                 getContentResolver().update(uri, values, null, null);
60             }
61         });
62         Button deleteData = (Button) findViewById(R.id.delete_data);
63         deleteData.setOnClickListener(new View.OnClickListener() {
64             @Override
65             public void onClick(View v) {
66             // 删除数据
67                 Uri uri = Uri.parse("content://com.mycompany.database.provider/book/" + newId);
68                         getContentResolver().delete(uri, null, null);
69             }
70         });
71     }
72 }

Author

发表评论

电子邮件地址不会被公开。 必填项已用*标注