我有2个表,分别是公司(用于存储登录信息)和任务(用于存储任务详细信息)。
公司表中的列:id(主键自动递增),uname,密码。 没有从用户那里获取ID。
工作分配表中的列:assignment_topic,assignment_content,total_marks,id(外键),id_assignment(主键自动递增)。
信息流: 1.登录用户->除了检查是否有有效用户外,还要初始化id(1000)并返回它(searchId()方法)。 2.用户从UI输入分配的详细信息-> BackgroundTask ---> insertAssignment方法。
问题:我无法通过从公司表中获取ID(主键)在分配表中插入ID(FOREIGN KEY)。有关列显示为空。
尝试了以下内容:
由于我是Android的新手,如果你们可以帮助我解决此问题,那就太好了。
如果您需要我提供进一步的信息,请告诉我。
下面是必要的代码。
Insert_assignment类:
public void SubmitData(View view) {
String topic_assign = topic.getText().toString();
String topic_content_assign = topic_content.getText().toString();
String total_marks = t_marks.getText().toString();
String id = loginInfo.getId();
Log.e("assignment_id:",id);
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute("add_assignment", topic_assign, topic_content_assign, total_marks,id);
}
BackgroundTask类:
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;
public class BackgroundTask extends AsyncTask<String, Teacher_Assignment, String> {
private static final String TAG = " Background task";
LoginInfo loginInfo = new LoginInfo();
Activity activity;
ListView listView;
AssignmentAdapter assignmentAdapter;
Teacher_Assignment teacher_assignment = new Teacher_Assignment();
private Context context;
BackgroundTask(Context context)
{
this.context = context;
activity = (Activity)context;
}
@Override
protected String doInBackground(String... params) {
String method = params[0];
DatabaseHelper databaseHelper = new DatabaseHelper(context);
if(method.equals("add_assignment")) {
String topic_assignment = params[1];
String topic_content = params[2];
String total_marks = params[3];
String id = params[4];
SQLiteDatabase db = databaseHelper.getWritableDatabase();
databaseHelper.insertAssignment(db,topic_assignment,topic_content,total_marks, id);
Log.d(TAG,"inserted successfully");
} else if(method.equals("get_info")) {
listView = (ListView) activity.findViewById(R.id.list_assignment);
//DatabaseHelper databaseHelper = new DatabaseHelper(context);
SQLiteDatabase db = databaseHelper.getReadableDatabase();
Cursor cursor = databaseHelper.getInformation(db);
assignmentAdapter = new AssignmentAdapter(context, R.layout.activity_assignment_page_recycle_view);
String assignment_content;
while(cursor.moveToNext()){
assignment_content = cursor.getString(cursor.getColumnIndex(teacher_assignment.Assignment_content));
Teacher_Assignment teacher_assignment = new Teacher_Assignment(assignment_content);
publishProgress(teacher_assignment);
}
return "get_info";
}
return "One row inserted";
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
if (result.equals("get_info")) {
listView.setAdapter(assignmentAdapter);
} else {
Toast.makeText(context, result, Toast.LENGTH_LONG).show();
}
}
protected void onProgressUpdate(Assignment_Page... values) {
assignmentAdapter.add(values[2]);
}
}
DatabaseHelper类:
package com.example.education;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.nfc.Tag;
import android.util.Log;
class DatabaseHelper extends SQLiteOpenHelper {
LoginInfo loginInfo = new LoginInfo();
Teacher_Assignment assign1 = new Teacher_Assignment();
//Login table
private static final int DATABASE_VERSION = 41;
private static final String DATABASE_NAME = "userInfo.db";
private static final String TABLE_NAME = "corporate";
private static final String COLUMN_ID = "id";
private static final String COLUMN_UNAME = "uname";
private static final String COLUMN_PASSWORD = "password";
private static final String TABLE_ASSIGNMENT = "assignment";
//private static final String COLUMN_ID_ASSIGNMENT = "user_id";
private static final String ASSIGNMENT_TOPIC = "assignment_topic";
//private static final String COLUMN_NAME = COLUMN_UNAME;
private static final String ASSIGNMENT_CONTENT = "assignment_content";
private static final String ASSIGNMENT_TOTAL_MARKS = "total_marks";
private static final String ID_COLUMN_ASSIGNMENT = "id_assignment";
private static final String TABLE_STUDENT_SELECTION = "student_selection";
private static final String COLUMN_STUDENT_ID = "student_id";
private static final String COLUMN_CLASS_SEC = "class_sec";
private static final String COLUMN_STUDENT_NAME = "name";
private static final String COLUMN_TOTAL_MARKS = "total_marks";
private static final String COLUMN_STUDENT_MARKS = "student_score";
SQLiteDatabase db;
private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_UNAME + " TEXT NOT NULL, " + COLUMN_PASSWORD + " TEXT NOT NULL " + ")";
private static final String CREATE_ASSIGNMENT_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_ASSIGNMENT + "("
+ ASSIGNMENT_TOPIC + " TEXT NOT NULL, " + ASSIGNMENT_CONTENT + " TEXT NOT NULL, "
+ ASSIGNMENT_TOTAL_MARKS + " TEXT NOT NULL, "
+ ID_COLUMN_ASSIGNMENT + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_ID + " TEXT " + ")";
//+ " FOREIGN KEY(" + COLUMN_ID + ") REFERENCES " + TABLE_NAME + " (id) "
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void insertData() {
db = this.getWritableDatabase();
ContentValues content = new ContentValues();
content.put(COLUMN_ID,1000);
content.put(COLUMN_UNAME, "akhil");
content.put(COLUMN_PASSWORD,"admin");
db.insert(TABLE_NAME, null, content);
db.close();
}
public String insertAssignment(SQLiteDatabase db,String topic, String assignment_content, String total_marks, String id) {
db = this.getWritableDatabase();
ContentValues content1 = new ContentValues();
//id = searchId(COLUMN_UNAME);
content1.put(ASSIGNMENT_TOPIC, topic);
content1.put(ASSIGNMENT_CONTENT,assignment_content);
content1.put(ASSIGNMENT_TOTAL_MARKS,total_marks);
content1.put(COLUMN_ID, loginInfo.getId());
db.insert(TABLE_ASSIGNMENT, null, content1);
db.close();
return topic;
}
数据库屏幕截图:
答案 0 :(得分:0)
id值来自在BackgroundTask中初始化的loginInfo实例:
class DatabaseHelper extends SQLiteOpenHelper {
LoginInfo loginInfo = new LoginInfo();
如果通过//id = searchId(COLUMN_UNAME);
获得了ID,请检查searchId方法。
答案 1 :(得分:0)
此问题已解决,方法是将数据从一个活动传递到另一个活动,然后调用SearchId函数