Как сделать данные из таблицы db sqlite значениями X и Y на линейной диаграмме? - PullRequest
0 голосов
/ 20 июня 2020

У меня есть таблица с внешним ключом в качестве категории ... например, таблица company состоит из имени сотрудника и зарплаты ... так что данные таблицы, подобные этой (Google) ---> peter, 20 $ (Google) --- > sam, 15 $

здесь проблемы, как сделать, чтобы этот peter, 20 $ и sam, 20 $ стали значениями X и Y в линейной диаграмме. Я сделал dbhelper, companyDAO, employeeDAO именно так

package com.example.myapplication.adapter;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper {

	public static final String TAG = "DBHelper";


	// columns of the companies table
	public static final String TABLE_COMPANIES = "companies";
	public static final String COLUMN_COMPANY_ID = "_id";
	public static final String COLUMN_COMPANY_NAME = "company_name";


	// columns of the employees table
	public static final String TABLE_EMPLOYEES = "employees";
	public static final String COLUMN_EMPLOYE_ID = COLUMN_COMPANY_ID;
	public static final String COLUMN_EMPLOYE_FIRST_NAME = "first_name";
	public static final String COLUMN_EMPLOYE_SALARY = "salary";
	public static final String COLUMN_EMPLOYE_COMPANY_ID = "company_id";

	private static final String DATABASE_NAME = "companies.db";
	private static final int DATABASE_VERSION = 1;

	// SQL statement of the employees table creation
	private static final String SQL_CREATE_TABLE_EMPLOYEES = "CREATE TABLE " + TABLE_EMPLOYEES + "("
			+ COLUMN_EMPLOYE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ COLUMN_EMPLOYE_FIRST_NAME + " TEXT NOT NULL, "
			+ COLUMN_EMPLOYE_SALARY + " REAL NOT NULL, "
			+ COLUMN_EMPLOYE_COMPANY_ID + " INTEGER NOT NULL "
			+");";

	// SQL statement of the companies table creation
	private static final String SQL_CREATE_TABLE_COMPANIES = "CREATE TABLE " + TABLE_COMPANIES + "("
			+ COLUMN_COMPANY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ COLUMN_COMPANY_NAME + " TEXT NOT NULL "
			+");";

	public DBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase database) {
		database.execSQL(SQL_CREATE_TABLE_COMPANIES);
		database.execSQL(SQL_CREATE_TABLE_EMPLOYEES);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(TAG, "Upgrading the database from version " + oldVersion + " to "+ newVersion);
		// clear all data
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_EMPLOYEES);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMPANIES);
		db.execSQL("DROP TABLE IF EXISTS " + COLUMN_COMPANY_NAME);

		// recreate the tables
		onCreate(db);
	}

	public DBHelper(Context context, String name, CursorFactory factory,int version) {
		super(context, DATABASE_NAME, factory, DATABASE_VERSION);
	}
}

package com.example.myapplication.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.myapplication.Company;
import com.example.myapplication.Employee;
import com.example.myapplication.adapter.DBHelper;

import java.util.ArrayList;
import java.util.List;


public class EmployeeDAO {

	public static final String TAG = "EmployeeDAO";

	private Context mContext;

	// Database fields
	private SQLiteDatabase mDatabase;
	private DBHelper mDbHelper;
	private String[] mAllColumns = { DBHelper.COLUMN_EMPLOYE_ID,
			DBHelper.COLUMN_EMPLOYE_FIRST_NAME,
			DBHelper.COLUMN_EMPLOYE_SALARY, DBHelper.COLUMN_EMPLOYE_COMPANY_ID };

	public EmployeeDAO(Context context) {
		mDbHelper = new DBHelper(context);
		this.mContext = context;
		// open the database
		try {
			open();
		} catch (SQLException e) {
			Log.e(TAG, "SQLException on openning database " + e.getMessage());
			e.printStackTrace();
		}
	}

	public void open() throws SQLException {
		mDatabase = mDbHelper.getWritableDatabase();
	}

	public void close() {
		mDbHelper.close();
	}

	public Employee createEmploye(String firstName,  double salary,
			long companyId) {
		ContentValues values = new ContentValues();
		values.put(DBHelper.COLUMN_EMPLOYE_FIRST_NAME, firstName);
		values.put(DBHelper.COLUMN_EMPLOYE_SALARY, salary);
		values.put(DBHelper.COLUMN_EMPLOYE_COMPANY_ID, companyId);
		long insertId = mDatabase
				.insert(DBHelper.TABLE_EMPLOYEES, null, values);
		Cursor cursor = mDatabase.query(DBHelper.TABLE_EMPLOYEES, mAllColumns,
				DBHelper.COLUMN_EMPLOYE_ID + " = " + insertId, null, null,
				null, null);
		cursor.moveToFirst();
		Employee newEmployee = cursorToEmploye(cursor);
		cursor.close();
		return newEmployee;
	}

	public void deleteEmployee(Employee employee) {
		long id = employee.getId();
		System.out.println("the deleted employee has the id: " + id);
		mDatabase.delete(DBHelper.TABLE_EMPLOYEES, DBHelper.COLUMN_EMPLOYE_ID
				+ " = " + id, null);
	}

	public List<Employee> getAllEmployees() {
		List<Employee> listEmployees = new ArrayList<Employee>();

		Cursor cursor = mDatabase.query(DBHelper.TABLE_EMPLOYEES, mAllColumns,
				null, null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Employee employee = cursorToEmploye(cursor);
			listEmployees.add(employee);
			cursor.moveToNext();
		}
		// make sure to close the cursor
		cursor.close();
		return listEmployees;
	}

	public List<Employee> getEmployeesOfCompany(long companyId) {
		List<Employee> listEmployees = new ArrayList<Employee>();

		Cursor cursor = mDatabase.query(DBHelper.TABLE_EMPLOYEES, mAllColumns,
				DBHelper.COLUMN_COMPANY_ID + " = ?",
				new String[] { String.valueOf(companyId) }, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Employee employee = cursorToEmploye(cursor);
			listEmployees.add(employee);
			cursor.moveToNext();
		}
		// make sure to close the cursor
		cursor.close();
		return listEmployees;
	}

	private Employee cursorToEmploye(Cursor cursor) {
		Employee employee = new Employee();
		employee.setId(cursor.getLong(0));
		employee.setFirstName(cursor.getString(1));
		employee.setSalary(cursor.getDouble(2));

		// get The company by id
		long companyId = cursor.getLong(3);
		CompanyDAO dao = new CompanyDAO(mContext);
		Company company = dao.getCompanyById(companyId);
		if (company != null)
			employee.setCompany(company);

		return employee;
	}

}

package com.example.myapplication.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.myapplication.Company;
import com.example.myapplication.Employee;
import com.example.myapplication.adapter.DBHelper;

import java.util.ArrayList;
import java.util.List;

public class CompanyDAO {

	public static final String TAG = "CompanyDAO";

	// Database fields
	private SQLiteDatabase mDatabase;
	private DBHelper mDbHelper;
	private Context mContext;
	private String[] mAllColumns = { DBHelper.COLUMN_COMPANY_ID,
			DBHelper.COLUMN_COMPANY_NAME };

	public CompanyDAO(Context context) {
		this.mContext = context;
		mDbHelper = new DBHelper(context);
		// open the database
		try {
			open();
		} catch (SQLException e) {
			Log.e(TAG, "SQLException on openning database " + e.getMessage());
			e.printStackTrace();
		}
	}

	public void open() throws SQLException {
		mDatabase = mDbHelper.getWritableDatabase();
	}

	public void close() {
		mDbHelper.close();
	}

	public Company createCompany(String name) {
		ContentValues values = new ContentValues();
		values.put(DBHelper.COLUMN_COMPANY_NAME, name);
		long insertId = mDatabase
				.insert(DBHelper.TABLE_COMPANIES, null, values);
		Cursor cursor = mDatabase.query(DBHelper.TABLE_COMPANIES, mAllColumns,
				DBHelper.COLUMN_COMPANY_ID + " = " + insertId, null, null,
				null, null);
		cursor.moveToFirst();
		Company newCompany = cursorToCompany(cursor);
		cursor.close();
		return newCompany;
	}

	public void deleteCompany(Company company) {
		long id = company.getId();
		// delete all employees of this company
		EmployeeDAO employeeDao = new EmployeeDAO(mContext);
		List<Employee> listEmployees = employeeDao.getEmployeesOfCompany(id);
		if (listEmployees != null && !listEmployees.isEmpty()) {
			for (Employee e : listEmployees) {
				employeeDao.deleteEmployee(e);
			}
		}

		System.out.println("the deleted company has the id: " + id);
		mDatabase.delete(DBHelper.TABLE_COMPANIES, DBHelper.COLUMN_COMPANY_ID
				+ " = " + id, null);
	}

	public List<Company> getAllCompanies() {
		List<Company> listCompanies = new ArrayList<Company>();

		Cursor cursor = mDatabase.query(DBHelper.TABLE_COMPANIES, mAllColumns,
				null, null, null, null, null);
		if (cursor != null) {
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {
				Company company = cursorToCompany(cursor);
				listCompanies.add(company);
				cursor.moveToNext();
			}

			// make sure to close the cursor
			cursor.close();
		}
		return listCompanies;
	}

	public Company getCompanyById(long id) {
		Cursor cursor = mDatabase.query(DBHelper.TABLE_COMPANIES, mAllColumns,
				DBHelper.COLUMN_COMPANY_ID + " = ?",
				new String[] { String.valueOf(id) }, null, null, null);
		if (cursor != null) {
			cursor.moveToFirst();
		}

		Company company = cursorToCompany(cursor);
		return company;
	}

	protected Company cursorToCompany(Cursor cursor) {
		Company company = new Company();
		company.setId(cursor.getLong(0));
		company.setName(cursor.getString(1));
		return company;
	}

}

Я также импортировал график из

спасибо

...