我是Slick的新手,并努力为以下人员找到一个好的规范示例。
我想在两个表中插入一行。第一个表有一个自动递增的主键。第二个表通过其主键与第一个表相关。
所以我想:
请欣赏上述逻辑的典型示例,以及下面我的定义的任何相关建议(我对Slick来说很新!)。谢谢!
private def insertAndReturn(entry: Entry) =
entries returning entries.map(_.id)
into ((_, newId) => entry.copy(id = newId))
def insert(entry: Entry): Future[Entry] =
db.run(insertAndReturn(entry) += entry)
(类似于表2)
class EntryTable(tag: Tag) extends Table[Entry](tag, "tblEntry") {
def id = column[EntryId]("entryID", O.PrimaryKey, O.AutoInc)
...
def * = (id, ...).shaped <> (Entry.tupled, Entry.unapply)
}
class UsernameChangeTable(tag: Tag) extends Table[UserNameChange](tag, "tblUserNameChange") {
def entryId = column[EntryId]("entryID")
...
def entry = foreignKey("ENTRY_FK", entryId, entryDao.entries)(
_.id, onUpdate = Restrict, onDelete = Cascade
)
我正在使用MySQL数据库和Slick 3.1.0。
答案 0 :(得分:5)
你所要做的就是
val tx =
insertAndReturn(entry).flatMap { id =>
insertUserNameChange(UserNameChange(id, ...))
}.transactionally
db.run(tx)
请注意,insertUserNameChange
是将UserNameChange
实例插入数据库的函数。它需要您从之前的插入操作中获得的EntryId。
使用flatMap
撰写操作并使用transactionally
在事务中运行整个查询。
你的Slick表看起来很好。
答案 1 :(得分:0)
以下是实现此功能的规范示例
public void DataData() {
try {
FirebaseOptions options = new FirebaseOptions.Builder()
.setApplicationId("1:673426911334:android:31ed7fa834e0c6f4") // Required for Analytics.
.setApiKey("AIzaSyD8o7uS21z1ZeN17cTdIjx6EQBV3lgJoQs") // Required for Auth.
.setDatabaseUrl("https://intracitydriver.firebaseio.com/") // Required for RTDB.
.build();
FirebaseApp.initializeApp(this, options, "sec");
FirebaseApp secondary = FirebaseApp.getInstance("sec");
FirebaseDatabase secondaryDatabase = FirebaseDatabase.getInstance(secondary);
secondaryDatabase.getReference("9728824163").addValueEventListener(new com.google.firebase.database.ValueEventListener() {
@Override
public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
for (com.google.firebase.database.DataSnapshot locationSnapshot : dataSnapshot.getChildren()) {`enter code here`
String location = locationSnapshot.getValue().toString();
String[] strings = location.split("=");
String aDouble = (strings[2]);
String aDouble2 = (strings[3]);
String[] newlat = aDouble.split(",");
String[] newlon = aDouble2.split("\\}");
Double lat = Double.valueOf(newlat[0]);
Double lon = Double.valueOf(newlon[0]);
// String s=strings[5];
Toast.makeText(Home.this, ""+lat+lon, Toast.LENGTH_SHORT).show();
Log.d("Locations updated", "location: " + location);
Log.e("Yogesh", lat + "hello" + lon);
final LatLng latLng = new LatLng(lat, lon);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// NEWTruckMarker(latLng);
}
}, 1000);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
} catch (Exception e) {'
// Toast.makeText(this, "" + e, Toast.LENGTH_SHORT).show();
e.printStackTrace();
Log.e("Crash Found: ",""+e);'
}
}